PyQt5的安装与配置

    技术2022-07-10  138

    1、PIP下载PyQt5

    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyqt5 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyqt5-tools

    2、配置PyQt5

    设置–工具–外部工具,配置如下内容:

    Name:QtDesigner Program:D:\study\python\Scripts\pyqt5designer.exe Working Directory:$FileDir$ Name:PyUIC Program:D:\study\python\Scripts\pyuic5.exe Arguments:$FileName$ -o $FileNameWithoutExtension$.py Working Directory:$FileDir$ Name:Pyqcc Program:D:\study\python\Scripts\pyrcc5.exe Arguments:$FileName$ -o $FileNameWithoutExtension$_rc.py Working Directory:$FileDir$

    3、第一个程序

    import sys from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(QtWidgets.QWidget): def __init__(self, Form): super().__init__() self.setupUi(Form) def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 300) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(150, 120, 75, 23)) self.pushButton.setObjectName("pushButton") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "PushButton")) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) form = QtWidgets.QWidget() window = Ui_Form(form) form.show() sys.exit(app.exec_())

    笔记

    from PyQt5.Qt import *

    没有设置父控件的控件,会被当做顶层控件

    顶层控件需要调用show()方法来显示

    每个顶层控件是一个独立的窗体 顶层控件会自带框架(如:标题、最大化、最小化和关闭按钮)

    子控件在父控件show()后,会自动显示

    槽函数中,使用self.sender()方法获取发送者

    QSS QT样式表,写法与CSS基本相同; 选择PropertyName的方法:[PropertyName=“xxx”]{…} 选择ObjectName的方法:#ObjectName{…}

    音乐无法播放的原因: Qt的多媒体播放的底层使用了DirectShowPlayerService,所以需要一个DirectShow解码器,例如Lav Filters,安装好解码器后,再运行程序尝试,就能播放出声音了。下载地址:https://lav-filters.en.lo4d.com/windows

    Processed: 0.019, SQL: 9