一、Designer-UI编辑器
Designer是pyqt5-tools带的工具,默认可以在Python安装目录下找到的。我的之前项目导入过pyqt5-tools,所以我直接全盘搜索到了。
打开designer后,我们可以编辑我们想要的UI界面,下面是我编辑的测试界面:
编辑好界面,点击保存 会生成一个.ui格式的文件。
二、将.ui文件转.py文件
使用Designer创建出来的.ui文件不能被python读取,所以我们需要转成.py格式
使用工具:pyuic5, 也在python的安装目录下。
1.在pyuic5所在目录打开命令行(若已将pyuic5加入环境变量,则忽略此步骤)。
2.然后执行以下命令:
1
pyuic5-oD:\test.pyD:\test.ui
生成test.py内容如下:
Python
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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Users\Administrator.000\Desktop\log.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
fromPyQt5importQtCore,QtGui,QtWidgets
classUi_MainWindow(object):
defsetupUi(self,MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800,600)
self.centralwidget=QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.printLogButton=QtWidgets.QPushButton(self.centralwidget)
self.printLogButton.setGeometry(QtCore.QRect(190,30,381,51))
self.printLogButton.setObjectName("printLogButton")
self.textEdit=QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(150,190,461,341))
self.textEdit.setObjectName("textEdit")
self.clearLogButton=QtWidgets.QPushButton(self.centralwidget)
self.clearLogButton.setGeometry(QtCore.QRect(190,110,381,51))
self.clearLogButton.setObjectName("clearLogButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar=QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0,0,800,23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar=QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
defretranslateUi(self,MainWindow):
_translate=QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow","MainWindow"))
self.printLogButton.setText(_translate("MainWindow","打印Log"))
self.clearLogButton.setText(_translate("MainWindow","清除Log"))
三、创建简单项目结构
我们新建个py项目,文件结构如下:
下面来填充代码:
Entry.py(入口py脚本)
Python
1
2
3
4
5
6
importMainController
if__name__=='__main__':
mainController=MainController.MainController()
mainController.do()
MainView.py(将上一步生成的test.py内容copy进来)
Python
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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Users\Administrator.000\Desktop\log.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
fromPyQt5importQtCore,QtGui,QtWidgets
classUi_MainWindow(object):
defsetupUi(self,MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800,600)
self.centralwidget=QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.printLogButton=QtWidgets.QPushButton(self.centralwidget)
self.printLogButton.setGeometry(QtCore.QRect(190,30,381,51))
self.printLogButton.setObjectName("printLogButton")
self.textEdit=QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(150,190,461,341))
self.textEdit.setObjectName("textEdit")
self.clearLogButton=QtWidgets.QPushButton(self.centralwidget)
self.clearLogButton.setGeometry(QtCore.QRect(190,110,381,51))
self.clearLogButton.setObjectName("clearLogButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar=QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0,0,800,23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar=QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
defretranslateUi(self,MainWindow):
_translate=QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow","MainWindow"))
self.printLogButton.setText(_translate("MainWindow","打印Log"))
self.clearLogButton.setText(_translate("MainWindow","清除Log"))
MainController.py(Control脚本,处理MainView逻辑)
Python
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
importMainView
importsys
importrandom
fromPyQt5.QtWidgetsimportQApplication
fromPyQt5.QtWidgetsimportQMainWindow
fromPyQt5importQtCore,QtGui,QtWidgets
classMainController():
defdo(self):
app=QApplication(sys.argv)
mainWindow=QMainWindow()
self.mainView=MainView.Ui_MainWindow()
self.mainView.setupUi(mainWindow)
self.mainView.printLogButton.clicked.connect(self.printLogClick)
self.mainView.clearLogButton.clicked.connect(self.clearLogClick)
# 重定向输出
sys.stdout=EmittingStream(textWritten=self.normalOutputWritten)
sys.stderr=EmittingStream(textWritten=self.normalOutputWritten)
mainWindow.show()
sys.exit(app.exec_())
defprintLogClick(self):
print(random.random())
defclearLogClick(self):
self.mainView.textEdit.clear()
defnormalOutputWritten(self,text):
cursor=self.mainView.textEdit.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.mainView.textEdit.setTextCursor(cursor)
self.mainView.textEdit.ensureCursorVisible()
classEmittingStream(QtCore.QObject):
textWritten=QtCore.pyqtSignal(str)
defwrite(self,text):
self.textWritten.emit(str(text))
当所有准备就绪后,我们来运行测试一下我们的成果吧~
最后编辑:2020-10-09作者:网虫虫
分享是一种快乐;
分享是一种美德;
分享是一种幸福!