??dialog_first.py 如下:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(416, 321)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(90, 100, 51, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(90, 150, 89, 24))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "主界面"))
self.label.setText(_translate("Form", "界面1"))
self.pushButton.setText(_translate("Form", "切换界面"))
??dialog_second.py 如下:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(416, 321)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(170, 160, 51, 16))
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "从界面"))
self.label.setText(_translate("Form", "界面2"))
??main.py 如下:
import dialog_first
import dialog_second
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QDialog
class Dialog_main():
def init(self, mainWindow, mainView):
self.mainWindow = mainWindow
self.mainView = mainView
self.mainView.setupUi(mainWindow)
self.mainView.pushButton.clicked.connect(self.printClick)
def printClick(self):
print("hello, world")
class Dialog_next():
def init(self, mainWindow, mainView):
self.mainWindow = mainWindow
self.mainView = mainView
self.mainView.setupUi(mainWindow)
def show_ui(self):
self.mainWindow.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainDialog = Dialog_main()
mainWindow = QDialog()
mainView = dialog_first.Ui_Form()
mainDialog.init(mainWindow, mainView)
mainWindow.show()
secondDialog = Dialog_next()
secondWindow = QDialog()
secondView = dialog_second.Ui_Form()
secondDialog.init(secondWindow, secondView)
mainView.pushButton.clicked.connect(secondDialog.show_ui)
sys.exit(app.exec_())
|