There are very few tutorials about PyQt5 on the Internet, especially the interface jump. I have studied it for a long time, and I will share it with you.
First, the main interface
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 #It should be noted that the second one in the jump interface must use the QDialog class, not QWidget, I don't know why, pay special attention to 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): #This is the code generated by PyQt Designer, very simple, drag controls, generate ui files, and then convert UIC into py files 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) : # This piece of attention is focused on jumping from the main interface to the Demo1 interface. The main interface is hidden. If the Demo interface is closed, the main interface process will trigger self.form.show() and the main interface will be displayed again. 45 self.form.hide( ) #If there is no self.form.show() sentence, the program will be closed after closing the Demo1 interface 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_())
2. Jump interface 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()
3. Jump interface 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_())