2022年 11月 13日

最新版python如何安装qt5_PyQt5教程 – pythonQt的安装和配置及版本间差异

目前发现的python下最好的图形库,wxpython、tkinter都觉得没这个好用。PyQt的核心是Qt(c++)库,一般不会占用太多时间的逻辑代码速度慢点,也不会成为瓶颈。同时,在使用方式上,PyQt并没有失去Python的优雅语法,快速开发的能力。

安装pythonQt

OS: Windows 7 64bits

python version: 3.4

Note:

1. 注意选择正确的Python版本和系统位数。

2. 安装PyQt并不需要一些文章中提到的先安装sip和Qt的过程,因为在安装文件中已经包括。

测试是否安装成功

import PyQt5

没报错说明安装成功

pythonQt Designer配置

pyqt提供了Qt designer来设计窗口界面,用起来也非常方便,虽然做出来的界面不那么华丽。

增加系统变量QT_QPA_PLATFORM_PLUGIN_PATH

D:\python3.4.2\Lib\site-packages\PyQt5\plugins

Note:这是你安装pyqt目录下的插件目录

pycharm中配置qtdesigner

PyCharm > settings > tools > external tools > +添加

Qt Designer的设置如图:

0818b9ca8b590ca3270a3433284dd417.png

Note:

1. Program选择PyQt安装目录中 designer.exe 的路径

2. Work directory 删除后使用变量 $FileDir$ (点击后面的 Insert macro 按钮来选择),我的是包目录E:\mine\python_workspace\PyQt

Note:$FileDir$ 指的是选中文件所在文件夹路径;$FileName$指的是选中文件的文件名

pycharm中配置PyUIC

用来将 Qt界面 转换成py代码

PyUIC的设置类似qt designer,如图

0818b9ca8b590ca3270a3433284dd417.png

program: Python安装路径

Parameters: -m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py

Work directory 使用变量 $FileDir$

配置完成后,pycharm菜单栏中tool的变化

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

Note:不要在不相关py文件打开时运行PyUIC,否则整个py文件就会永久修改不能恢复,为了防止这种失误,可以在setting中的external tools中,将PyUIC前面的√去掉,这样菜单栏中就不会出现了。

PyQT5和PyQt4的区别

Porting to Qt5 has two aspects:

1) Getting the core PythonQt to build (that should be merely adjusting the qmake profile and maybe some incompatibilities in QMetaObject and friends)

2) Getting the Qt 4 wrappers to work (run the pythonqt_generator and see what happens… Some extra includes and fixes in the typesystem might be required)

The Qt wrappers could be done in two ways: Qt4 backward compatible (With widgetsbeing located in PythonQt.QtGui) or Qt5 only (with widgets being located in a new PythonQt.QtWidgets package).

PyQt5的Widget被分离出来,似乎用来替代Dialog,并将Widget放入了QtWidget模块(库)中,PyQt4是QtGUI。

I would also think it might be useful to watch what PySide and PyQt are doing in that respect (google for “PySide Qt5” or “PyQt Qt5”). So if you only require the core PythonQt library (without complete Qt wrappers), it should be the work of 1-2 days and you can contribute the code to PythonQt. If you aim for the complete wrappers, using Qt4 wrappers is the easiest but will take some tweaking to adapt. Generating real Qt5 wrappers is more work.