目前发现的python下最好的图形库,wxpython、tkinter都觉得没这个好用。PyQt的核心是Qt(c++)库,一般不会占用太多时间的逻辑代码速度慢点,也不会成为瓶颈。同时,在使用方式上,PyQt并没有失去Python的优雅语法,快速开发的能力。
安装pythonQt
Linux下安装pyqt4
export SIP_URL=https://sourceforge.net/projects/pyqt/files/sip/sip-4.18.1/sip-4.18.1.tar.gz
export PYQT4_URL=http://sourceforge.net/projects/pyqt/files/PyQt4/PyQt-4.11.4/PyQt-x11-gpl-4.11.4.tar.gz
export VIRENV=~/ENV/ubuntu_env
#export PYTHON_PATH=$VIRENV/bin/python
export PYTHON_PATH=python3
export OPT=~/opt
echo -e "\033[31;1m *****进入虚拟环境****** \033[0m"
#. $VIRENV/bin/activate
echo -e "\033[31;1m ************安装pyqt4依赖sip************* \033[0m"
test -f ../install/sip-*tar.gz || wget $SIP_URL -P ../install
test -d $OPT/sip* && rm -rf $OPT/sip*
tar -zxf ../install/sip-*tar.gz -C $OPT 1> /dev/null
cd $OPT/sip*
$PYTHON_PATH configure.py
#$PYTHON_PATH configure.py --incdir=$VIRENV/include/python
echo -e "\033[31;1m **************make************************ \033[0m"
make 1> /dev/null
echo -e "\033[31;1m *****************make install********************* \033[0m"
sudo make install 1> /dev/null
cd -
echo -e "\033[31;1m *************安装pyqt4其它依赖*********** \033[0m"
sudo apt-get install -y qt4-qmake 1> /dev/null
sudo apt-get install -y libqt4-dev 1> /dev/null
sudo apt-get install -y libqtwebkit-dev 1> /dev/null
echo -e "\033[31;1m ************安装pyqt4*************** \033[0m"
test -f ../install/PyQt-x11-gpl-4*.tar.gz || wget $PYQT4_URL -P ../install
test -f $OPT/PyQt* && rm -rf $OPT/PyQt*
tar -zxf ../install/PyQt-x11-gpl-4*.tar.gz -C $OPT 1> /dev/null
cd $OPT/PyQt*
$PYTHON_PATH configure-ng.py --qmake /usr/bin/qmake-qt4
#sudo $PYTHON_PATH configure.py --verbose
echo -e "\033[31;1m **************make************************ \033[0m"
make 1> /dev/null
echo -e "\033[31;1m *****************make install********************* \033[0m"
sudo make install 1> /dev/null
cd -
检查是否安装成功
import PyQt4
安装出错及解决
sip make install
cp: cannot remove '/home/pipi/ENV/ubuntu_env/include/python3.5m/sip.h': Permission denied
make[1]: *** [install] Error 1
make: *** [install] Error 2
解决:sudo make install
python configure.py
Error: /usr/bin/qmake failed to create a makefile. Make sure you have a working Qt qmake on your PATH or use the -q argument to explicitly specify a working Qt qmake.
主要是因为没有安装qt4的qmake
Perhaps you do not have qmake installed. On Fedora this is in the Qt -devel package, but on debian / ubuntu it may be in it's own package; the two most likely candidates are:
>apt-cache search qmake qt3-dev-tools-Qt3development tools qt4-qmake-Qt4qmakeMakefilegenerator tool解决:sudo apt-get install -y qt4-qmake
fatal error: QCoreApplication: No such file or directory compilation terminated.
qtdetail.cpp:1:28: fatal error: QCoreApplication: No such file or directory compilation terminated.
qtdetail.mk:201: recipe for target 'qtdetail.o' failed
make: *** [qtdetail.o] Error 1
Error: Failed to determine the detail of your Qt installation.
PyQt-x11-gpl-4.11.4/qtdetail.pro:3: Parse Error ('/usr/bin/qmake-qt4')
Error processing project file: qtdetail.pro
Error: Failed to determine the detail of your Qt installation.
解决:python configure.py --qmake /usr/bin/qmake-qt4
warning: parameter ‘sipPy’ set but not used [-Wunused-but-set-parameter]
这个不用管,pyqt4已经安装成功了
from PyQt4.QtWebKit import QWebView ImportError: No module named 'PyQt4.QtWebKit'
查看pyqt4安装目录下是否有QtWebKit.so, ls env/lib/python3.5/site-packages/PyQt4$sudo apt-get install -y libqt4-dev
sudo apt-get install -y libqtwebkit-dev
然而再重装pyqt4, 要重新,否则无效!
Windows下安装pyqt5
OS: Windows 7 64bits
python version: 3
PyQt5下载 Download,选择PyQt5-5.5.1-gpl-Py3.4-Qt5.5.1-x64.exeWindows 64 bit installer下载并安装
Note:
1. 注意选择正确的Python版本和系统位数。
2. 安装PyQt并不需要一些文章中提到的先安装sip和Qt的过程,因为在安装文件中已经包括。
pyqt直接用exe文件安装较好,pip安装比较复杂。
测试是否安装成功
import PyQt5
没报错说明安装成功
皮皮blog
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的设置如图:
Note:
1. Program选择PyQt安装目录中 designer.exe 的路径
2. Work directory 删除后使用变量 $FileDir$ (点击后面的 Insert macro 按钮来选择),我的是包目录E:\mine\python_workspace\PyQt
Note:$FileDir$ 指的是选中文件所在文件夹路径;$FileName$指的是选中文件的文件名
pycharm中配置PyUIC
{用来将 Qt界面xml代码 转换成py代码}
PyUIC的设置类似qt designer,如图
program: Python安装路径
Parameters: -m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Work directory 使用变量 $FileDir$
配置完成后,pycharm菜单栏中tool的变化:
pycharm中配置Pyrcc
{这个是用于将qrc资源文件转换成py文件的工具的配置}
如果在qt designer中使用了qrc资源文件,而没有将其转换为py文件就会出错:import error no module named resource_rc
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.
附录
TkInter的使用