PyQt5实现界面跳转

网上关于PyQt5的教程很少,特别是界面跳转这一块儿,自己研究了半天,下来和大家分享一下

一、首先是主界面


1
# -*- coding: utf-8 -*- 2 3 # Form implementation generated from reading ui file 'Form.ui' 4 # 5 # Created by: PyQt5 UI code generator 5.10.1 6 # 7 # WARNING! All changes made in this file will be lost! 8 #要注意的是跳转界面第二个必须使用QDialog类,不能使用QWidget,我也不知道为什么,特别注意 9 from PyQt5 import QtCore, QtGui, QtWidgets 10 from PyQt5.QtWidgets import QMainWindow, QApplication 11 import Dialog1 12 import Dialog2 13 import sys 14 15 16 class Ui_Form(object): #这是用PyQt Designer生成的代码,很简单的,拖动控件,生成ui文件,然后UIC转换成py文件 17 def setupUi(self, Form): 18 Form.setObjectName("Form") 19 Form.resize(440, 310) 20 self.form = Form 21 self.btn_d1 = QtWidgets.QPushButton(Form) 22 self.btn_d1.setGeometry(QtCore.QRect(60, 140, 75, 23)) 23 self.btn_d1.setObjectName("btn_d1") 24 self.btn_d2 = QtWidgets.QPushButton(Form) 25 self.btn_d2.setGeometry(QtCore.QRect(180, 140, 75, 23)) 26 self.btn_d2.setObjectName("btn_d2") 27 self.btn_exit = QtWidgets.QPushButton(Form) 28 self.btn_exit.setGeometry(QtCore.QRect(310, 140, 75, 23)) 29 self.btn_exit.setObjectName("btn_exit") 30 31 self.retranslateUi(Form) 32 QtCore.QMetaObject.connectSlotsByName(Form) 33 34 def retranslateUi(self, Form): 35 _translate = QtCore.QCoreApplication.translate 36 Form.setWindowTitle(_translate("Form", "Form")) 37 self.btn_d1.setText(_translate("Form", "Demo1")) 38 self.btn_d1.clicked.connect(self.jump_to_demo1) 39 self.btn_d2.setText(_translate("Form", "Demo2")) 40 self.btn_d2.clicked.connect(self.jump_to_demo2) 41 self.btn_exit.setText(_translate("Form", "Exit")) 42 self.btn_exit.clicked.connect(self.exit) 43 44 def jump_to_demo1(self): #这一块注意,是重点从主界面跳转到Demo1界面,主界面隐藏,如果关闭Demo界面,主界面进程会触发self.form.show()会再次显示主界面 45 self.form.hide() #如果没有self.form.show()这一句,关闭Demo1界面后就会关闭程序 46 form1 = QtWidgets.QDialog() 47 ui = Dialog1.Ui_Dialog1() 48 ui.setupUi(form1) 49 form1.show() 50 form1.exec_() 51 self.form.show() 52 53 def jump_to_demo2(self): 54 self.form.hide() 55 form2 = QtWidgets.QDialog() 56 ui = Dialog2.Ui_Dialog2() 57 ui.setupUi(form2) 58 form2.show() 59 form2.exec_() 60 self.form.show() 61 62 def exit(self): 63 self.form.close() 64 65 66 if __name__ == "__main__": 67 app = QApplication(sys.argv) 68 form = QtWidgets.QWidget() 69 window = Ui_Form() 70 window.setupUi(form) 71 form.show() 72 sys.exit(app.exec_())

二、跳转界面Demo1

 1 # -*- coding: utf-8 -*-
 2 
 3 # Form implementation generated from reading ui file 'Dialog1.ui'
 4 #
 5 # Created by: PyQt5 UI code generator 5.10.1
 6 #
 7 # WARNING! All changes made in this file will be lost!
 8 
 9 from PyQt5 import QtCore, QtGui, QtWidgets
10 
11 class Ui_Dialog1(object):
12     def setupUi(self, Dialog1):
13         Dialog1.setObjectName("Dialog1")
14         Dialog1.resize(400, 300)
15         self.dialog=Dialog1
16         self.pushButton = QtWidgets.QPushButton(Dialog1)
17         self.pushButton.setGeometry(QtCore.QRect(140, 140, 75, 23))
18         self.pushButton.setObjectName("pushButton")
19 
20         self.retranslateUi(Dialog1)
21         QtCore.QMetaObject.connectSlotsByName(Dialog1)
22 
23     def retranslateUi(self, Dialog1):
24         _translate = QtCore.QCoreApplication.translate
25         Dialog1.setWindowTitle(_translate("Dialog1", "Dialog"))
26         self.pushButton.setText(_translate("Dialog1", "Jump to main"))
27         self.pushButton.clicked.connect(self.jump_to_main)
28 
29     def jump_to_main(self):
30         self.dialog.close()

三、跳转界面Demo2

 1 # -*- coding: utf-8 -*-
 2 
 3 # Form implementation generated from reading ui file 'Dialog2.ui'
 4 #
 5 # Created by: PyQt5 UI code generator 5.10.1
 6 #
 7 # WARNING! All changes made in this file will be lost!
 8 
 9 from PyQt5 import QtCore, QtGui, QtWidgets
10 from PyQt5.QtWidgets import QMainWindow, QDialog, QApplication
11 import sys
12 
13 
14 class Ui_Dialog2(object):
15     def setupUi(self, Dialog2):
16         Dialog2.setObjectName("Dialog2")
17         Dialog2.resize(400, 300)
18         self.dialog = Dialog2
19         self.pushButton = QtWidgets.QPushButton(Dialog2)
20         self.pushButton.setGeometry(QtCore.QRect(140, 160, 75, 23))
21         self.pushButton.setObjectName("pushButton")
22 
23         self.retranslateUi(Dialog2)
24         QtCore.QMetaObject.connectSlotsByName(Dialog2)
25 
26     def retranslateUi(self, Dialog2):
27         _translate = QtCore.QCoreApplication.translate
28         Dialog2.setWindowTitle(_translate("Dialog2", "Dialog"))
29         self.pushButton.setText(_translate("Dialog2", "Jump to main"))
30         self.pushButton.clicked.connect(self.go_main)
31 
32     def go_main(self):
33         self.dialog.close()
34 
35 if __name__ == "__main__":
36     app = QApplication(sys.argv)
37     form = QtWidgets.QDialog()
38     ui = Ui_Dialog2()
39     ui.setupUi(form)
40     form.show()
41     sys.exit(app.exec_())

猜你喜欢

转载自www.cnblogs.com/qdzj/p/8974660.html