呆萌教你:使用Python 开发串口工具 1. 开发环境的搭建-QtDesigner在PyCharm中的配置
?开发环境的搭建
?1. PyQt5的安装
# 在Terminal 输入如下命令 pip3 install PyQt5==5.13 -i https://pypi.tuna.tsinghua.edu.cn/simple
?2. PyQt5_tools的安装
QtDesigner 可以使用鼠标拖曳的方式完成界面的设计,方便快捷,相比使用代码设计界面更加友好。
pip3 install PyQt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple
安装完毕后,需要将QtDesigner添加到PyCharm软件中,这样方便以后界面的设计和UI文件的编译。
- QtDesigner的添加
QtDesigner用于界面设计,打开PyCharm,File—>Settings—>External Tools,点击加号来添加自己的工具,做如下配置:
Name:QtDesigner Group:Qt Programs:C:\anaconda\Library\bin\designer.exe(这里是designer路径,位于anaconda安装目录下 anaconda—>Libra—>bin) Working directory:$ProjectFileDir$

- Pyuic的添加
Pyuic用于对图像界面进行编译,编译成 .py的文件,这样写代码时就可以直接import,配置如下:
Name:Pyuic Group:Qt Program:C:\anaconda\python.exe(自己的python路径) Arguments:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py Working directory:$FileDir$

?3. PyCharm中的测试
- 如图,打开PyCharm—>Tools—>Qt会出现两个菜单,第一个是界面设计,第二个是编译工具

- 点击QtDesigner进入图形界面编辑,编辑完毕后保存.ui文件,默认保存到当前项目下。

- 回到PyCharm中,选中刚刚生成的xxx.ui 文件,这里一定要注意点中该文件后再进行如下操作,PyCharm—>Tools—>Qt—>Pyuic,点击Pyuic后,项目文件夹中会出现xxx.py的文件,这个文件就是编译出来的py文件。
- 这时候就可以import该文件,我的ui文件名为:MES_Login。界面右键,Debug或Run就可以看到设计的界面了,所做即所得。
import sys from PyQt5.QtWidgets import QMainWindow, QApplication import MES_Login class Login(QMainWindow): def __init__(self, parent=None): super(Login, self).__init__(parent) self.ui = MES_Login.Ui_Form() self.ui.setupUi(self) if __name__== "__main__": app = QApplication(sys.argv) # # 界面实例化 Win_login = Login() # 界面显示 Win_login.show() sys.exit(app.exec_())
?下一节我们将使用QtDesigner对串口工具的页面进行设计。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/173363.html原文链接:https://javaforall.net
