3

Centos 7安装Pyqt5、Qt Designer以及配置Vscode教程

 2 years ago
source link: https://segmentfault.com/a/1190000040165680
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Centos 7安装Pyqt5、Qt Designer以及配置Vscode教程

发布于 今天 22:45

最近准备学习Python GUI,除了要写代码的实现外,目前认为最快捷的方式估计是PyQt了,在配置环境的过程中,网上的教程都是基于Windows的,也有基于Ubuntu的,但一直没有找到CentOS下的教程,走了不少弯路,经过摸索,终于搞定。
Ubuntu下的安装方式:

sudo apt-get install qttools5-dev-tools

but:......在centos下出错。。。

一、Python安装

这一部分没什么好说的,网上有大量的教程,按图索骥就行。

二、安装Pyqt5

PyQt5 有两种安装方式,一种是从官网下载源码安装,另外一种是使用 pip 安装。

这里我推荐大家使用pip 安装。因为它会自动根据你的Python 版本来选择合适的 PyQt5 版本,如果是手动下载源码安装,难免会选择出错。建议使用比较稳妥的安装方式。

pip3 install PyQt5

一般这种方式在国内的环境会比较慢,有可能会提示安装失败,这里建议使用国内安装源(豆瓣)的方式解决:

pip install PyQt5 -i https://pypi.douban.com/simple

三、安装 PyQt5-tools

同样使用国内镜像安装

pip install PyQt5-tools -i https://pypi.douban.com/simple

四、配置Vscode

1.安装pyqt integration扩展

2.配置pyqt integration
这里有个Qt designer路径的设置问题,因为我们没有单独安装Qt designer,安装PyQt5-tools的时候已经安装好Qt designer了,每台电脑的路径又不尽相同,所以刚开始寻找起来比较麻烦,这里推荐使用Linux find命令来定位相关文件的位置。

find / -name designer

找到QT designer路径后,复制并配置在pyqt integration里

在文件管理,空白处右键选择PYQT:New Form建立表单

打开QT Designer,并创建一个表单保存ui文件,返回Vscode

右键选中刚才创建的ui文件,选择PYQT:Compile Form,生成同名的python文件

图片.png

生成的代码:

图片.png

尝试运行刚刚生成的“Ui_mainus.py”是没用的,因为生成的文件并没有程序入口。因此我们在同一个目录下另外创建一个程序叫做“main.py”,并输入如下内容,将Ui_untitled替换为你生成.py文件名。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

import Ui_mainus

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_mainus.Ui_Dialog()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

图片.png

至此,我们在Centos 7完成了安装Pyqt5、Qt Designer以及配置Vscode的任务,我也在学习Pyqt5的过程中,欢迎大家讨论指教。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK