使用python3+PyQt5+PyCharm桌面GUI开发

下面简单介绍基于Python3、PyQt5和PyCharm进行桌面GUI开发。

环境配置

安装python3

PyQt5所支持的python版本是从3.5开始的,因此安装的Python3版本必须大于3.5。

安装PyQt5

1
2
pip3 install PyQt5
pip3 install PyQt5-tools

安装PyCharm

安装完PyCharm后,需要配置Qt Designer和PyUIC,前者用于设计UI,后者用于将UI转成Py文件。

具体步骤请参考程序员之路:python3+PyQt5+pycharm桌面GUI开发

简单示例

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
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'clearWaterPrintGui.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.label_DirPath = QtWidgets.QLabel(Form)
self.label_DirPath.setGeometry(QtCore.QRect(16, 52, 72, 15))
self.label_DirPath.setObjectName("label_DirPath")
self.label_ExtName = QtWidgets.QLabel(Form)
self.label_ExtName.setGeometry(QtCore.QRect(31, 112, 72, 15))
self.label_ExtName.setObjectName("label_ExtName")
self.buttonBox = QtWidgets.QDialogButtonBox(Form)
self.buttonBox.setGeometry(QtCore.QRect(190, 260, 193, 28))
self.buttonBox.setInputMethodHints(QtCore.Qt.ImhNone)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.lineEdit_DirPath = QtWidgets.QLineEdit(Form)
self.lineEdit_DirPath.setGeometry(QtCore.QRect(100, 50, 281, 21))
self.lineEdit_DirPath.setObjectName("lineEdit_DirPath")
self.lineEdit_ExtName = QtWidgets.QLineEdit(Form)
self.lineEdit_ExtName.setGeometry(QtCore.QRect(100, 112, 281, 21))
self.lineEdit_ExtName.setObjectName("lineEdit_ExtName")

self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label_DirPath.setText(_translate("Form", "文件路径:"))
self.label_ExtName.setText(_translate("Form", "扩展名:"))

# 下面代码是手动添加的
if __name__=="__main__":
import sys
from PyQt5.QtGui import QIcon
app=QtWidgets.QApplication(sys.argv)
widget=QtWidgets.QWidget()
ui=Ui_Form()
ui.setupUi(widget)
# widget.setWindowIcon(QIcon('web.png'))#增加icon图标,如果没有图片可以没有这句
widget.show()
sys.exit(app.exec_())

参考链接

  1. 程序员之路:python3+PyQt5+pycharm桌面GUI开发,by 莫水千流.
  2. 使用PyQt快速开发GUI应用,by Prayer.
  3. PyQt,by wikipedia.
  4. Qt Widgets、QML、Qt Quick的区别,by 云水.
  5. Q_OBJECT宏的作用,by 沈子恒.