from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQT_demo.switch_interface.first import First_Ui_Form
from PyQT_demo.switch_interface.main import Ui_MainWindow
from PyQT_demo.switch_interface.second import Second_Ui_Form
from PyQT_demo.switch_interface.third import Third_Ui_Form
class MyWindow(QMainWindow, Ui_MainWindow):
dict_ = {
'pushButton': [0, 'first'],
'pushButton_2': [1, 'second'],
'pushButton_3': [2, 'third']
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.first = First_Ui_Form()
self.second = Second_Ui_Form()
self.third = Third_Ui_Form()
self.setupUi(self)
# 多个页面的绑定
for k, v in self.dict_.items():
form = QWidget()
# 将各个子页面添加到对应控件form
eval('self.{}.setupUi(form)'.format(v[1]))
# 在stackedWidget添加子控件form
self.stackedWidget.addWidget(form)
# 将按钮连接同一个槽函数
eval('self.{}.clicked.connect(self.show_page)'.format(k))
# 展示index的页面
def show_page(self):
index = self.dict_[self.sender().objectName()][0]
self.stackedWidget.setCurrentIndex(index)
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyWindow()
myWin.show()
sys.exit(app.exec())
1.第一个子页面(first.py)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'first.ui'
# Created by: PyQt5 UI code generator 5.9.2
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class First_Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(589, 491)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(130, 80, 371, 301))
self.widget.setObjectName("widget")
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setGeometry(QtCore.QRect(80, 110, 133, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.lineEdit.setText(_translate("Form", "first"))
2.第二个子页面(second.py)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'second.ui'
# Created by: PyQt5 UI code generator 5.9.2
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Second_Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(538, 524)
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(210, 160, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.lineEdit.setText(_translate("Form", "second"))
3.第三个子页面用(third.py)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'third.ui'
# Created by: PyQt5 UI code generator 5.9.2
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Third_Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(591, 391)
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(220, 110, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.lineEdit.setText(_translate("Form", "third"))
4.主页面(main_show.py)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
# Created by: PyQt5 UI code generator 5.9.2
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(792, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(40, 130, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(40, 190, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(40, 260, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
self.stackedWidget.setGeometry(QtCore.QRect(149, 30, 481, 451))
self.stackedWidget.setObjectName("stackedWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 792, 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)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))