Pycharm配置QtPy工具环境
找到setting安装第三方模块PyQT5,点加号,先安PyQT5,再安装pyqt5-tools,后面包含qtdesinger
以上模块都安完,设置扩展工具的参数找到setting->tools->external tools,点击加号新建工具
安装好安装PyQt5后,需要用同样的方法安装pyqt5-tools,安装PyQt5后没有designer.exe就是因为没有安装pyqt5-tools。安装好PyQt5后,designer.exe可执行程序时不在PyQt5中的根目录的,而是在“./Python/Lib/site-packages\pyqt5_tools\Qt\bin
”目录中。
安装好PyQt5模块包好后,在开发工具PyCharm中配置PyQt5工具(该配置主要有两个作用,一个是用来在开发工具PyCharm中能直接启动QT,一个是在开发工具PyCharm中将QT的.ui文件转换.py文件):在开发工具PyCharm打开“File”——“Settings”——“Tools”——“External Tools”在右侧单击添加按钮,如下图所示:
1.安装PyQt5
pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple
2.PyQt5.9之后不再提供常用的Qt工具,比如图形界面开发工具Qt Designer、国际化翻译工具Liguist,所以还需要使用如下命令安装常用的Qt工具。
pip install PyQt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple
安装完的包在%\python37\Lib\site-packages
目录下。
2.设置环境变量
为了让Windows系统能够正确识别PyQt5-tools的常用命令,还需要把PyQt5-tools的安装目录添加到系统环境变量Path中。
右击我的电脑->属性->高级系统设置->环境变量->系统变量->编辑
新建->添加pyqt5-toolss的路径。
3.基于pycharm搭建开发环境
File->settings->Tools->External Tools
Name:Qt Designer
Program:F:\Users\LCJ\AppData\Local\Programs\Python\Python37\Lib\site-packages\pyqt5_tools\Qt\bin\designer.exe
Working directory:F:\Users\LCJ\AppData\Local\Programs\Python\Python37\Lib\site-packages\pyqt5_tools\Qt\bin
作用为打开QT Designer,name为标识,只要你知道就可以随便取,program为designer.exe的位置,Working directory为designer.exe所在的文件夹
Name:PyUI
Program:F:\Users\LCJ\AppData\Local\Programs\Python\Python37\python.exe
Parameters:-m PyQt5.uic.pyuic F i l e N a m e FileNameFileName -o F i l e N a m e W i t h o u t E x t e n s i o n FileNameWithoutExtensionFileNameWithoutExtension.py
Working directory:$FileDir$
作用为将qt代码转化为.py的文件。
4.使用PyQt5
Tools->External Tools->Qt Designer->main window->建立一个工程->保存为test.ui
选择刚才创建好的文件->右键->External Tools->PyUI
生成test.py后再新建一个py程序。
import sys
import test
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = test.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())