我们需要安装相关的 Python 依赖包,首先安装 PyQt5:
pip install PyQt5
安装常用的 Qt 工具:
pip install PyQt5-tools
将 PyQt5-tools
的路径(D:\Anaconda3_Environments\envs\...\Lib\site-packages\pyqt5_tools
)添加到环境变量中:
现在我们尝试能否导入 PyQt5
,不报错说明安装成功:
import PyQt5
print(help(PyQt5))
在 File-Settings-Tools
选项卡中点击 External Tools
,然后点击加号添加 Qt Designer
和 pyuid
两个选项,其中 Qt Designer
填写的内容如下:
Name: 自定义
Program: Qt Designer的安装路径(D:\...\site-packages\qt5_applications\Qt\bin\designer.exe)
Arguments: 不填
Working directory: $FileDir$
pyuic
填写的内容如下:
Name: 自定义
Program: pyuic的安装路径(D:\Anaconda3_Environments\envs\...\Scripts\pyuic5.exe)
Arguments: $FileName$ -o $FileNameWithoutExtension$.py
Working directory: $FileDir$
完成后即可在 PyCharm 的 Tools-External Tools
打开 Qt Designer:
我们创建一个简单的 UI 界面后保存至项目根目录的 UI
目录下:
然后需要将 test.ui
文件转换成 .py
文件,方便使用 PyCharm 查看修改,在 .ui
文件上右键,在 External Tools
中选择 pyuic
,即可看到转换完成后的 .py
文件:
但是此时的文件是不能运行的,还需要在其前后添加相应的执行代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1280, 720)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(1090, 580, 160, 80))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1280, 21))
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", "OK"))
self.pushButton_2.setText(_translate("MainWindow", "Cancel"))
# ---------- 添加以下代码运行程序 ----------
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
现在运行 .py
代码即可看到设计的界面: