Python

QT+SIP+Pyqt5

2019-02-18  本文已影响32人  上发条的树

参考

安装环境

需要安装的

开始安装

安装QT

$ brew install qt
==> Downloading https://homebrew.bintray.com/bottles/qt-5.12.1.mojave.bottle.tar
######################################################################## 100.0%
==> Pouring qt-5.12.1.mojave.bottle.tar.gz
==> Caveats
We agreed to the Qt open source license for you.
If this is unacceptable you should uninstall.

qt is keg-only, which means it was not symlinked into /usr/local,
because Qt 5 has CMake issues when linked.

If you need to have qt first in your PATH run:
  echo 'export PATH="/usr/local/opt/qt/bin:$PATH"' >> ~/.bash_profile

For compilers to find qt you may need to set:
  export LDFLAGS="-L/usr/local/opt/qt/lib"
  export CPPFLAGS="-I/usr/local/opt/qt/include"

==> Summary
🍺  /usr/local/Cellar/qt/5.12.1: 9,658 files, 319MB

其中,最后的/usr/local/Cellar/qt/5.12.1为解压路径,后面会用到。

安装sip

$ pip3 install sip
Collecting sip
  Downloading https://files.pythonhosted.org/packages/b1/6f/782b6ff2770ebc6709dd7dc53a0636599a035853c78d785911fefc3f0e43/sip-4.19.8-cp36-cp36m-macosx_10_6_intel.whl (51kB)
    100% |████████████████████████████████| 61kB 147kB/s 
Installing collected packages: sip
Successfully installed sip-4.19.8

安装PyQt5

$ pip3 install pyqt5
Collecting pyqt5
  Downloading https://files.pythonhosted.org/packages/40/77/e79ffb62b9f356180f74dc6fe2b063a0c641004d42f59a1846bdcd1f7b5e/PyQt5-5.12-5.12.1_a-cp35.cp36.cp37.cp38-abi3-macosx_10_6_intel.whl (40.3MB)
    100% |████████████████████████████████| 40.3MB 116kB/s 
Collecting PyQt5_sip<4.20,>=4.19.14 (from pyqt5)
  Downloading https://files.pythonhosted.org/packages/ae/51/f4beda8b92e86dced117cf8242e7545f224756eb3778c10d5e0697d427b1/PyQt5_sip-4.19.14-cp36-cp36m-macosx_10_6_intel.whl (51kB)
    100% |████████████████████████████████| 61kB 128kB/s 
Installing collected packages: PyQt5-sip, pyqt5
Successfully installed PyQt5-sip-4.19.14 pyqt5-5.12

可以看到执行pip3 install pyqt5

成功安装了两个库:

配置PyCharm

1、切换到Python3.x环境

打开PyCharm,快捷键commad+,打开Python环境设置界面。
下拉菜单中即可选择电脑上对应的Python版本。

pycharm中Python版本设置.png

可以看到此Python版本已经自动加入PyQt5的库了,如果没有自动加入,需要点击“+”号,搜索PyQt5引入。

2、配置GUI设计工具

开始配置GUI设计工具.png 配置GUI设计工具.png

从上面步骤,我们可以找到安装QT的文件路径,在该路径下,找到Designer.app的文件,该文件的完整路径为/usr/local/Cellar/qt/5.12.1/libexec/Designer.app

此工具,通过PyCharm -> Tools -> External Tools ->此步骤设置的名称
即可调出QT的UI拖取工具。

3、配置UI文件编译工具

设置UI文件编译工具.png
$ echo $PATH
-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py

最终的操作,其实就是将.ui文件转为同名的.py文件:

python3 -m PyQt5.uic.pyuic xxx.ui -0 xxx.py

至此,环境和工具算是搭建完成了:


工具配置完成.png

点击Apply则开始执行工具配置:


点击apply开始配置工具.png

编写第一个PyQt5的GUI工具

创建窗口文件

打开GUI工具.png

请忽略我工程中的venv文件,该文件为PyCharm创建工程时候自动生成,此项目不依赖此虚拟环境,最终我会删除该文件。

创建一个GUI窗口文件:

创建一个GUI窗口文件.png

在窗口中拖一个控件label

拖控件.png

将该窗口文件保存到工程目录中去,我命名该文件为FirstTestView,后缀是自动生成的.ui

将窗口.ui文件编译成.py文件

如图,在项目中,右击FirstTestView.ui文件,选择External Tools -> PyGUI
生成同名的py文件:

屏幕快照 2019-02-18 下午2.45.07.png 生成同名py文件.png

创建一个工程的main文件

内容如下:

import FirstTestView

from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
if __name__=='__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = FirstTestView.Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

运行此文件,即可得到如下效果:

运行效果.png

至此,完成QT+SIP+Pyqt5的环境搭建以及第一个Pyqt5的GUI测试项目。

上一篇下一篇

猜你喜欢

热点阅读