通过点击左边的列表选项, 通过绑定按钮,使得右侧的堆栈窗口控件的界面显示发生变化
QStackedWidget.py
""" 堆栈窗口控件(QStackedWidget) """ import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class StackedExample(QWidget): def __init__(self): super(StackedExample, self).__init__() self.setGeometry(300, 50, 10, 10) self.setWindowTitle("堆栈窗口控件 (QStackedWidget)") self.list = QListWidget() self.list.insertItem(0, "联系方式") self.list.insertItem(1, "个人信息") self.list.insertItem(2, "教育程度") self.stack1 = QWidget() self.stack2 = QWidget() self.stack3 = QWidget() self.tab1UI() self.tab2UI() self.tab3UI() #构造堆栈窗口 self.stack = QStackedWidget() #将界面添加到窗口上 self.stack.addWidget(self.stack1) self.stack.addWidget(self.stack2) self.stack.addWidget(self.stack3) hbox = QHBoxLayout() hbox.addWidget(self.list) hbox.addWidget(self.stack) self.setLayout(hbox) #对于列表进行点击绑定 self.list.currentRowChanged.connect(self.display) def tab1UI(self): layout = QFormLayout() layout.addRow("姓名", QLineEdit()) layout.addRow("地址", QLineEdit()) # 在界面上添加对应的控件 self.stack1.setLayout(layout) def tab2UI(self): layout = QFormLayout() sex = QHBoxLayout() sex.addWidget(QRadioButton("男")) sex.addWidget(QRadioButton("女")) layout.addRow(QLabel('性别'), sex) layout.addRow("生日", QLineEdit()) self.stack2.setLayout(layout) def tab3UI(self): layout = QHBoxLayout() layout.addWidget(QLabel("科目")) layout.addWidget(QCheckBox("物理")) layout.addWidget(QCheckBox("高数")) self.stack3.setLayout(layout) def display(self, index): #设置当前的堆栈界面显示为第几个界面 self.stack.setCurrentIndex(index) if __name__ == "__main__": app = QApplication(sys.argv) main = StackedExample() main.show() sys.exit(app.exec_())