1.安装PyQt5 和 PyQt5-tools
pip install PyQt5 pip install PyQt5-tools
2.配置Pycharm
打开Pycharm,打开File -> Settings -> Tools -> External Tools,点击左上角 + ,开始配置Qt Designer 和 PyUIC。
(配图是已配置好的)
1)配置Qt Designer
在Qt Desgner的设置中,Program 选择pyqt5_tools目录下的 designer.exe 的路径
Working directory 使用变量 $ProjectFileDir$
2)配置PyUIC -- 用来将Qt界面转化为python代码
在PyUIC的设置中,Program选择 python 的地址,
Working directory 使用变量 $FileDir$ ,
Parameters填入 -m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
3)配置完成,打开Tools -> External Tools,可以看到配置好的QtDesigner和PyUIC
3.创建第一个PyQt5应用
1)打开Tools -> External Tools -> QtDeginer ,默认选择,点击 Creat 创建一个新的UI 。
2)这里摆放两个button ,然后命名保存文件。
3) 使用PyUIC转化为python代码 ,在test01.ui文件上点击右键,选择PyUIC,生成同名py文件。
4)生成的python代码 test01.py:
# Form implementation generated from reading ui file 'test01.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(40, 30, 56, 17)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(120, 30, 56, 17)) self.pushButton_2.setObjectName("pushButton_2") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 18)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "Button1")) self.pushButton_2.setText(_translate("MainWindow", "Button2"))
5)添加以下代码运行:
import sys from PyQt5.QtWidgets import QApplication,QMainWindow if __name__ == '__main__': app = QApplication(sys.argv) window = QMainWindow() ui = Ui_MainWindow() ui.setupUi(window) window.show() sys.exit(app.exec_())
6) 运行后的效果: