环境:Pycharm IDE+PtQT+Qt designer
第一步 安装环境
??需要安装python下qt的环境及工具,终端terminal下面运行指令,我这里已经安装,所以显示已经有了,两个都需要安装,需要记住安装的位置,一会儿需要设置环境变量。
pip install PyQt5
pip install PyQt5-tools
(DemoProject) E:\WorkSpace\BT\DemoProject>pip install PyQt5
Requirement already satisfied: PyQt5 in c:\users\guoqing.zhang\appdata\local\programs\python\python37\lib\site-packages (5.1
5.2)
Requirement already satisfied: PyQt5-sip<13,>=12.8 in c:\users\guoqing.zhang\appdata\local\programs\python\python37\lib\site
-packages (from PyQt5) (12.8.1)
配置qt designer.exe环境变量,根据自己安装的目录选择
目录:C:\Users\guoqing.zhang\AppData\Local\Programs\Python\Python37\Lib\site-packages\qt5_applications\Qt\bin
第二步 Pycharm配置外部工具
??打开Pycharm,点击File–>Setting–>Tools–>External Tools,选择+号新建,第一个是qt designer,program为路径,arguments不填,working directory 为工作路径,填 $ FileDir $,为当前工作目录,这个工具可以直接打开qt designer(qt 设计师)。
??第二个是PyUIC,program为python路径,arguments 填:-m PyQt5.uic.pyuic $ FileName $ -o $FileName,可以将UI文件转换为python代码,注意FileName前后没有空格。
第三步 qt界面设计
- 可以使用代码进行绘制
## 部分实例代码,文末下载整体代码self.Seed = QLineEdit()self.DescriptionSeed = QLabel()self.Key = QLineEdit()self.DescriptionKey = QLabel()self.DescriptionSeed.setText('Seed')self.DescriptionKey.setText('Key')self.Seed.setFixedWidth(200)self.Seed.setFixedHeight(25)self.Key.setFixedHeight(25)self.Key.setFixedWidth(200)self.Seed.setPlaceholderText('例如:0D 4A 1F FE')if __name__ == "__main__":app = QApplication(sys.argv)AllGlogol._init()mywindow = Ui_Dialog()mywindow.show()sys.exit(app.exec())
几个函数介绍一下
- setFixedSize,设置控件大小
- QHBoxLayout,QVBoxLayout,控件横纵摆放函数
- addWidget,添加控件函数
- addLayout,添加布局函数
- QGroupBox,管理布局的组管理函数
- setLayout,设置布局,就可以显示出控件
- 也可以直接使用qt designer 进行绘制,然后使用外部工具PyUIC转化成.py代码,然后运行即可,这里采用的是mainwindow。
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *import os
import sysclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(800, 600)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.tbnOpen = QtWidgets.QPushButton(self.centralwidget)self.tbnOpen.setGeometry(QtCore.QRect(190, 20, 101, 41))self.tbnOpen.setObjectName("tbnOpen")self.tbnClose = QtWidgets.QPushButton(self.centralwidget)self.tbnClose.setGeometry(QtCore.QRect(312, 20, 121, 41))self.tbnClose.setObjectName("tbnClose")self.label_1 = QtWidgets.QLabel(self.centralwidget)self.label_1.setGeometry(QtCore.QRect(20, 80, 191, 41))self.label_1.setObjectName("label_1")self.cBPortName = QtWidgets.QComboBox(self.centralwidget)self.cBPortName.setGeometry(QtCore.QRect(20, 20, 151, 41))self.cBPortName.setObjectName("cBPortName")self.tEReceiveData = QtWidgets.QTextEdit(self.centralwidget)self.tEReceiveData.setGeometry(QtCore.QRect(20, 110, 761, 371))self.tEReceiveData.setObjectName("tEReceiveData")self.lESend = QtWidgets.QLineEdit(self.centralwidget)self.lESend.setGeometry(QtCore.QRect(20, 510, 631, 31))self.lESend.setObjectName("lESend")self.label_2 = QtWidgets.QLabel(self.centralwidget)self.label_2.setGeometry(QtCore.QRect(20, 480, 191, 41))self.label_2.setObjectName("label_2")self.tbnSend = QtWidgets.QToolButton(self.centralwidget)self.tbnSend.setGeometry(QtCore.QRect(670, 510, 111, 31))self.tbnSend.setObjectName("tbnSend")self.label_Time = QtWidgets.QLabel(self.centralwidget)self.label_Time.setGeometry(QtCore.QRect(470, 24, 301, 31))self.label_Time.setObjectName("label_Time")self.tbnClear = QtWidgets.QToolButton(self.centralwidget)self.tbnClear.setGeometry(QtCore.QRect(670, 70, 111, 31))self.tbnClear.setObjectName("tbnClear")self.pBSelect = QtWidgets.QPushButton(self.centralwidget)self.pBSelect.setGeometry(QtCore.QRect(540, 70, 101, 31))self.pBSelect.setObjectName("pBSelect")self.pushButton = QtWidgets.QPushButton(self.centralwidget)self.pushButton.setGeometry(QtCore.QRect(430, 70, 93, 28))self.pushButton.setObjectName("pushButton")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))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.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.tbnOpen.setText(_translate("MainWindow", "打开端口"))self.tbnClose.setText(_translate("MainWindow", "关闭端口"))self.label_1.setText(_translate("MainWindow", "数据接收"))self.label_2.setText(_translate("MainWindow", "数据发送"))self.tbnSend.setText(_translate("MainWindow", "发送"))self.label_Time.setText(_translate("MainWindow", "TextLabel"))self.tbnClear.setText(_translate("MainWindow", "清空数据"))self.pBSelect.setText(_translate("MainWindow", "选择画笔"))self.pushButton.setText(_translate("MainWindow", "PushButton"))class Ui_Demo(QtWidgets.QMainWindow):def __init__(self):super(Ui_Demo, self).__init__()self._ui = Ui_MainWindow()self._ui.setupUi(self)if __name__ == '__main__':app = QApplication(sys.argv)MainWindow = Ui_Demo()MainWindow.show()sys.exit(app.exec_())
第四步打包生成exe文件
终端 terminal 下面运行指令:
??pyinstaller -F -w win.py,
运行完成之后,工程目录中会有dist的文件夹,里面有exe问可执行文件。
碰到的问题:Pycharm 右键菜单选项里面没有external tools
解决办法:复制Link或者path(如下图所示),然后到IDE界面显示工程文件的区域粘贴,反复尝试几次,然后重新打开就有了
工程地址下载:csdn
工程地址下载:github