PyQt_界面切换

  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_())

PyQt_界面切换_第1张图片

你可能感兴趣的:(ui,python,PyQt)