尝试过用各种 python 的 gui 库来写一些小工具, TkInter, wxPython, Pyfltk , PyQt等, 最终发现还是只有 wxPython 和 PyQt 能相对靠谱一些, 控件全, 文档丰富, 使用的人多. 因为曾经使用搞过 qt , 所以最终选择了 PyQt, 这次我们来说一下如何在 Mac 上安装.
1. 安装 qt
PyQt 其实就是 qt 的 python 绑定, 所以我们首先需要安装 qt, 版本可以自行选择, 最新版 的下载地址, 历史版本的地址. 如果追求最新版的话, 最好是去 PyQt 的下载网站看那一下最新版是什么, 因为 PyQt 的更新速度会落后于 qt.

当看到这个界面时, qt 就安装成功了.
2. 安装 sip
sip 是一个 python 调用 c 的工具, 官方网址在这里, 我们可以按照官网上的指南去安装, 也可以选择另一种更简单的方式:
| 1 | brew install sip | 
3. 安装 PyQt
我们首先去这里下载适合自己版本, 解压, 并在终端 cd 进这个目录. 执行:
| 1 | python configure.py | 
如果出现 Error: Use the --qmake argument to explicitly specify a working Qt qmake. 错误, 则是因为我们没有将 qmake 加入到环境变量中. 那么 qmake 在哪里呢? 根绝安装路径和版本,差不多是在这样的一个路径中:

知道了路径, 我们可以似乎可以通过 --qmake 参数指定 qmake 目录 ,但其实我们可以把它临时的加入到环境变量中, 在终端中键入:
| 1 | export PATH=/path/your/qt/version/clang_64/bin:$PATH | 
再次执行python configure.py, 一切顺林的话会遇到一个选择 license 的提示, 我们输入 yes 即可.
等待 configure 完成, 我们执行:1
make
这次可能并没有那么顺利, 我遇到这样的错误:
| 1 | In file included from ../qpy/QtCore/qpycore_api.h:30: | 
这时候我们需要传入 sip 的 include 路径, 如果是通过 brew 安装的(如果不是, 请看文章末尾的Q&A), 可以通过 brew info sip获得:
| 1 | $ brew info sip | 
然后重新执行
| 1 | python configure.py --sip-incdir=/path/of/your/sip/4.16.9/include | 
等待成功后, 我们可以执行:
| 1 | make && make install | 
到此为止, 我们安装就完成了. 下面让我们用 zetcode 上的例题测试一下:
| 1 | #!/usr/bin/python3 | 
把上面那段代码保存在一个python文件中, 然后执行:
| 1 | python test.py | 
如果出现下面这个界面, 就说明你成功了!

如果你没有通过 brew 安装 sip , 你可能遇到的问题:
1. 安装 PyQt , 执行 python configure.py 时找不到 sip
| 1 | sh: sip: command not found | 
解决方案:
知道你安装 sip make install 时的一些信息, 比如:1
2$ make install
cp -f sip /usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/bin/sip
然后在终端中执行:
| 1 | export PATH=/path/of/your/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/bin/:$PATH | 
2. fatal error: ‘sip.h’ file not found
错误:1
2
3
4
5
6
7
8In file included from qpycore_post_init.cpp:25:
In file included from ../qpy/QtCore/qpycore_api.h:30:
../qpy/QtCore/qpycore_public_api.h:26:10: fatal error: 'sip.h' file not found
#include <sip.h>
         ^
1 error generated.
make[1]: *** [qpycore_post_init.o] Error 1
make: *** [sub-QtCore-make_first-ordered] Error 2
解决方案:
| 1 | python configure.py --sip-incdir=/path/of/your/Downloads/sip-4.16.9/siplib | 
Update 2015年11月25日:
如何为 python3 安装 pyqt5
1. 安装 sip 时附加额外参数 --with-python3
| 1 | brew install sip --with-python3 | 
2. 安装 PyQt 时附加额外参数 --target-py-version=VERSION (e.g. 3.4)
| 1 | python3 configure.py --sip-incdir=/path/of/your/sip/include --target-py-version=VERSION |