一直想写一些桌面软件,公司电脑是 Ubuntu,个人电脑是 Mac,Windows 又是用户量最多的,想来想去使用跨平台的开发工具来写才是唯一的选择。

最终选择使用 PyQt,把 Python 的便利和 Qt 的兼容完美结合。

所需软件:

  1. Python3
  2. Qt5
  3. SIP
  4. PyQt5

安装 Python3

1
brew install python3

Python 环境搭建相关可参考:Python - Mac 的环境搭建

安装 Qt5.5

官网下载后打开安装后,把 bin 目录加入环境变量

终端安装 Pyqt

Mac 上使用编译安装,后面使用pyinstaller打包会出问题,推荐使用命令行一键安装,Linux 如果包管理没有的话,就使用编译安装吧。

1
brew install pyqt5

编译安装 Pyqt

安装 SIP

sip-4.17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$ python3 configure.py
This is SIP 4.17 for Python 3.5.1 on darwin.
The SIP code generator will be installed in
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/bin.
The sip module will be installed in
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages.
The sip.h header file will be installed in
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m.
The default directory to install .sip files in is
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/share/sip.
Creating siplib/sip.h...
Creating siplib/siplib.c...
Creating siplib/siplib.sbf...
Creating sipconfig.py...
Creating top level Makefile...
Creating sip code generator Makefile...
Creating sip module Makefile...

$ make
cc -c -pipe -Os -w -DNDEBUG -I. -o main.o main.c
cc -c -pipe -Os -w -DNDEBUG -I. -o transform.o transform.c
cc -c -pipe -Os -w -DNDEBUG -I. -o gencode.o gencode.c
cc -c -pipe -Os -w -DNDEBUG -I. -o extracts.o extracts.c
cc -c -pipe -Os -w -DNDEBUG -I. -o export.o export.c
cc -c -pipe -Os -w -DNDEBUG -I. -o heap.o heap.c
cc -c -pipe -Os -w -DNDEBUG -I. -o parser.o parser.c
cc -c -pipe -Os -w -DNDEBUG -I. -o lexer.o lexer.c
c++ -headerpad_max_install_names -o sip main.o transform.o gencode.o extracts.o export.o heap.o parser.o lexer.o
cc -c -pipe -fPIC -Os -w -DNDEBUG -I. -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -o siplib.o siplib.c
cc -c -pipe -fPIC -Os -w -DNDEBUG -I. -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -o apiversions.o apiversions.c
cc -c -pipe -fPIC -Os -w -DNDEBUG -I. -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -o descriptors.o descriptors.c
cc -c -pipe -fPIC -Os -w -DNDEBUG -I. -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -o qtlib.o qtlib.c
cc -c -pipe -fPIC -Os -w -DNDEBUG -I. -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -o threads.o threads.c
cc -c -pipe -fPIC -Os -w -DNDEBUG -I. -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -o objmap.o objmap.c
cc -c -pipe -fPIC -Os -w -DNDEBUG -I. -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -o voidptr.o voidptr.c
cc -c -pipe -fPIC -Os -w -DNDEBUG -I. -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -o array.o array.c
c++ -c -pipe -fPIC -Os -w -DNDEBUG -I. -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -o bool.o bool.cpp
c++ -headerpad_max_install_names -bundle -undefined dynamic_lookup -o sip.so siplib.o apiversions.o descriptors.o qtlib.o threads.o objmap.o voidptr.o array.o bool.o

$ sudo make install
cp -f sip /usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/bin/sip
cp -f sip.so /usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sip.so
cp -f /Users/Gavin/Downloads/PyQt/sip-4.17/siplib/sip.h /usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m/sip.h
cp -f sipconfig.py /usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sipconfig.py
cp -f /Users/Gavin/Downloads/PyQt/sip-4.17/sipdistutils.py /usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sipdistutils.py

最终安装在/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/bin/sip,记住这个地址,安装 PyQt5 会用到。

安装 PyQt5

PyQt-gpl-5.5.1
1
2
3
4
5
6
7
8
9
10
11
$ python3 configure.py -q /Users/Gavin/Develop/Qt-5.5.1/5.5/clang_64/bin/qmake -d /usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages --sip /usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/bin/sip

# -q 为 Qt 的 qmake 路径
# -d 为 python3.5 的 site-packages,可通过 import site; site.getsitepackages() 来查看
# --sip 为 sip 的路径

$ make

# 时间比较长

$ sudo make install

可能出现的错误:fatal error: 'qgeolocation.h' file not found
下载‘qgeolocation.h’放入PyQt-gpl-5.5.1/QtPositioning/即可

Hello PyQt

hello.py
1
2
3
4
5
6
7
8
9
10
11
import sys
from PyQt5 import QtCore, QtGui, QtWidgets

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Hello PyQt')
w.show()
sys.exit(app.exec_())
1
python3 hello.py

完美运行,成功搭建好开发环境,Ubuntu 的搭建和 Mac 类似,Windows 下载 PyQt5 的 Binary Packages 一键就可以搭建好。

enjoy it~