PyQt5/PySide2是Qt环境的python实现,Qt可以实现跨平台的GUI编程。
PyQt5/PySide2环境需要包含 Anaconda(Python3环境),PyQt5/PySide2,eric6(把Qt的GUI设计文件转换为python文件),QtDesigner(Qt的GUI生成器)
1 Anaconda 2019.03
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
安装完成之后更换清华国内源:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
2 PyQt5 /PySide2
PyQt是GPLv3协议,大意是你的程序中用了它,你的程序就要开源,如果闭源商用就会违反协议(后果自负,脸皮够厚无所谓)。除非你搞封装动态加载那一套来强行规避。
PySide是LGPL协议,如果你只是作为库用用它,你的程序还是可以闭源商用。
所以很多人喜欢PySide。如果不做商业项目,强烈建议使用PyQt,资料多,稳定。需要开发闭源商用软件的就用PySide。
GPL(General Public License)和LGPL( Lesser General Public License)是GNU的两种License。越来越多的自由软件(Free Software)使用GPL作为其授权声明,如果对GPL一点都不了解,有可能在使用自由软件时违反了GPL的授权。如果是个人或不正规的公司倒也无所谓,但如果是有规模的公司,恐怕会有被起诉的风险。
LGPL是GPL的变种,也是GNU为了得到更多的甚至是商用软件开发商的支持而提出的。与 GPL的最大不同是,可以私有使用LGPL授权的自由软件,开发出来的新软件可以是私有的而不需要是自由软件。所以任何公司在使用自由软件之前应该保证在 LGPL或其它GPL变种的授权下。
PyQt5/PySide2 可以通过Python的pip命令安装。
(1)利用conda命令创建虚拟环境
conda create -n qt python=3
(base) zhou@ZHOU-PC:~$ conda create -n qt python=3
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /home/zhou/Documents/soft/anaconda3/envs/qt
added / updated specs:
- python=3
The following NEW packages will be INSTALLED:
ca-certificates anaconda/pkgs/main/linux-64::ca-certificates-2019.5.15-0
certifi anaconda/pkgs/main/linux-64::certifi-2019.6.16-py37_0
libedit anaconda/pkgs/main/linux-64::libedit-3.1.20181209-hc058e9b_0
libffi anaconda/pkgs/main/linux-64::libffi-3.2.1-hd88cf55_4
libgcc-ng anaconda/pkgs/main/linux-64::libgcc-ng-9.1.0-hdf63c60_0
libstdcxx-ng anaconda/pkgs/main/linux-64::libstdcxx-ng-9.1.0-hdf63c60_0
ncurses anaconda/pkgs/main/linux-64::ncurses-6.1-he6710b0_1
openssl anaconda/pkgs/main/linux-64::openssl-1.1.1c-h7b6447c_1
pip anaconda/pkgs/main/linux-64::pip-19.1.1-py37_0
python anaconda/pkgs/main/linux-64::python-3.7.3-h0371630_0
readline anaconda/pkgs/main/linux-64::readline-7.0-h7b6447c_5
setuptools anaconda/pkgs/main/linux-64::setuptools-41.0.1-py37_0
sqlite anaconda/pkgs/main/linux-64::sqlite-3.28.0-h7b6447c_0
tk anaconda/pkgs/main/linux-64::tk-8.6.8-hbc83047_0
wheel anaconda/pkgs/main/linux-64::wheel-0.33.4-py37_0
xz anaconda/pkgs/main/linux-64::xz-5.2.4-h14c3975_4
zlib anaconda/pkgs/main/linux-64::zlib-1.2.11-h7b6447c_3
Proceed ([y]/n)?
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate qt
#
# To deactivate an active environment, use
#
# $ conda deactivate
(base) zhou@ZHOU-PC:~$ conda activate qt
(qt) zhou@ZHOU-PC:~$
(2) PyQt5/PySide2
PyQt5/PySide2可以通过pip命令安装,可以只安装其中一种即可,目前多为安装PyQt5。
Anaconda中自带的PyQt5版本为5.9.X,版本较低,现在最新的版本为5.12.X,因此若要安装最新的版本则必须使用Python自带的pip命令安装。
pip install pyside2
(qt) zhou@ZHOU-PC:~$ pip install pyside2
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting pyside2
Downloading https://mirrors.aliyun.com/pypi/packages/3d/a1/9300c616621ba1d1a3426d98b59cd5403045eafaa0263e68e7d4d48a49e4/PySide2-5.13.0-5.13.0-cp35.cp36.cp37-abi3-manylinux1_x86_64.whl (152.9MB)
|████████████████████████████████| 152.9MB 1.5MB/s
Collecting shiboken2==5.13.0 (from pyside2)
Downloading https://mirrors.aliyun.com/pypi/packages/d5/e2/e121fa55a759a286588a4017f57f34c03e8b35722131a2189a4f0e84bbac/shiboken2-5.13.0-5.13.0-cp35.cp36.cp37-abi3-manylinux1_x86_64.whl (792kB)
|████████████████████████████████| 798kB 1.6MB/s
Installing collected packages: shiboken2, pyside2
Successfully installed pyside2-5.13.0 shiboken2-5.13.0
安装PySide2时会自动安装依赖包shiboken2
PyQt5环境通过另外一种方式安装(安装eric6时自动安装)
3 QtDesigner
注意:PyQt5.9以后的版本不在包含QtDesigner,需要自行安装,在Windows环境下可以通过pip命令直接安装pyqt5-tools,里面就包含了QtDesigner等工具,但是在Linux或MacOS环境下该命令不适用,最好的解决办法是安装完整版本的Qt开发环境。
QtDesigner需要安装Qt环境,Deepin15.10环境自带的Qt环境为4.9.x,最新Qt环境为5.12.x,因此要使用最新环境需要到官网下载安装包。
http://download.qt.io/archive/qt/
下载完成之后安装Qt
QtDesigner位置:
/home/zhou/Documents/soft/Qt5.12.4/5.12.4/gcc_64/bin/designer
QtDesigner需要记住,后面eric6中还需要指定QtTools文件位置
4 eric6
eric6需要到官网下载安装包。
https://sourceforge.net/projects/eric-ide/files/eric6/stable/
(1)安装eric6
文件下载完成之后解压,终端命令行模式进入该文件运行命令
python install.py
(qt) zhou@ZHOU-PC:~/Documents/soft/eric6$ python install.py
Checking dependencies
Python Version: 3.7.3
PyQt5 could not be detected.
Error: No module named 'PyQt5'
Shall 'PyQt5' be installed using pip? (Y/n)
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting PyQt5
Downloading https://mirrors.aliyun.com/pypi/packages/83/d0/b4860e06095c4e1e60294365bd0d2a0d3e3d4b9b64bcd0241f083ffc01f2/PyQt5-5.12.3-5.12.4-cp35.cp36.cp37.cp38-abi3-manylinux1_x86_64.whl (61.9MB)
|████████████████████████████████| 62.0MB 1.7MB/s
Collecting PyQt5_sip<13,>=4.19.14 (from PyQt5)
Downloading https://mirrors.aliyun.com/pypi/packages/fe/4a/d995f9565b4cf1fa91781e377175b118b7c2bcb46e7c8f946e2745ee3e7f/PyQt5_sip-4.19.17-cp37-cp37m-manylinux1_x86_64.whl (67kB)
|████████████████████████████████| 71kB 1.5MB/s
Installing collected packages: PyQt5-sip, PyQt5
Successfully installed PyQt5-5.12.3 PyQt5-sip-4.19.17
Found PyQt5
Found pyuic5
PyQtWebEngine could not be detected.
Error: cannot import name 'QtWebEngineWidgets' from 'PyQt5' (/home/zhou/Documents/soft/anaconda3/envs/qt/lib/python3.7/site-packages/PyQt5/__init__.py)
Shall 'PyQtWebEngine' be installed using pip? (Y/n)
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting PyQtWebEngine
Downloading https://mirrors.aliyun.com/pypi/packages/da/fb/aa8344730c31174ffc81453da2d8ad2a626e618915529da5c73185ccca89/PyQtWebEngine-5.12.1-5.12.4-cp35.cp36.cp37.cp38-abi3-manylinux1_x86_64.whl (60.4MB)
|████████████████████████████████| 60.4MB 1.7MB/s
Requirement already satisfied: PyQt5>=5.12 in /home/zhou/Documents/soft/anaconda3/envs/qt/lib/python3.7/site-packages (from PyQtWebEngine) (5.12.3)
Requirement already satisfied: PyQt5_sip<13,>=4.19.14 in /home/zhou/Documents/soft/anaconda3/envs/qt/lib/python3.7/site-packages (from PyQt5>=5.12->PyQtWebEngine) (4.19.17)
Installing collected packages: PyQtWebEngine
Successfully installed PyQtWebEngine-5.12.1
QScintilla could not be detected.
Error: cannot import name 'Qsci' from 'PyQt5' (/home/zhou/Documents/soft/anaconda3/envs/qt/lib/python3.7/site-packages/PyQt5/__init__.py)
Shall 'QScintilla' be installed using pip? (Y/n)
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting QScintilla
Downloading https://mirrors.aliyun.com/pypi/packages/61/40/942a155ac1476861befbcff8ab7f73e233aba48f88af1bf58e98f9794ec3/QScintilla-2.11.2-5.12.4-cp35.cp36.cp37.cp38-abi3-manylinux1_x86_64.whl (2.7MB)
|████████████████████████████████| 2.7MB 1.5MB/s
Requirement already satisfied: PyQt5>=5.10.1 in /home/zhou/Documents/soft/anaconda3/envs/qt/lib/python3.7/site-packages (from QScintilla) (5.12.3)
Requirement already satisfied: PyQt5_sip<13,>=4.19.14 in /home/zhou/Documents/soft/anaconda3/envs/qt/lib/python3.7/site-packages (from PyQt5>=5.10.1->QScintilla) (4.19.17)
Installing collected packages: QScintilla
Successfully installed QScintilla-2.11.2
Found QScintilla2
Found QtGui
Found QtNetwork
Found QtPrintSupport
Found QtSql
Found QtSvg
Found QtWidgets
Found QtWebEngineWidgets
Qt Version: 5.12.4
sip Version: 4.19.17
PyQt Version: 5.12.3
QScintilla Version: 2.11.2
All dependencies ok.
Cleaning up old installation ...
Creating configuration file ...
Compiling user interface files ...
Compiling source files ...
Installing eric6 ...
Installing Python API files to '/home/zhou/Documents/soft/anaconda3/envs/qt/lib/python3.7/site-packages/eric6/api/python'.
Installing Ruby API files to '/home/zhou/Documents/soft/anaconda3/envs/qt/lib/python3.7/site-packages/eric6/api/ruby'.
Installing QSS API files to '/home/zhou/Documents/soft/anaconda3/envs/qt/lib/python3.7/site-packages/eric6/api/qss'.
Installation complete.
默认会安装PyQt5及依赖关系PyQt-sip和QScintilla,以及PyQtWebEngine。
(2)配置eric6
终端命令行中输入: eric6,启动eric6。eric6从18.xx版本不在支持中文界面。
“Setting - Preferences”,打开首选项页面。
② 设置API
③ 设置自动补全
④ 设置自动检查时间间隔,默认为5秒
⑤ 设置字体大小
⑥ 设置Typing
⑦ 设置工作目录
⑧ 设置QtTools