当前位置:   article > 正文

PyQt5组件之QStackedWidget_pyqt5 qstackedwidget

pyqt5 qstackedwidget

QStackedWidget简介

QTackedWidget是一个堆栈窗口控件,可以填充一些小控件,但是同一时间只有一个小控件可以显示,QStackedWidget使用QStackedLayout布局。QSTackedWidget控件与QTabWidget类似,可以有效的显示窗口的控件

QStackedWidget效果截图:

 

 

PyQt 设计器截图: 

  *.ui 转换为*.py 代码 

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'untitled13.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.15.4
  5. #
  6. # WARNING: Any manual changes made to this file will be lost when pyuic5 is
  7. # run again. Do not edit this file unless you know what you are doing.
  8. import sys
  9. from PyQt5 import QtCore, QtGui, QtWidgets
  10. from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidget, QWidget, QFormLayout, QLineEdit, QHBoxLayout, \
  11. QRadioButton, QLabel, QCheckBox
  12. class Ui_MainWindow(object):
  13. def setupUi(self, MainWindow):
  14. MainWindow.setObjectName("MainWindow")
  15. MainWindow.resize(800, 600)
  16. self.centralwidget = QtWidgets.QWidget(MainWindow)
  17. self.centralwidget.setObjectName("centralwidget")
  18. self.verticalStackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
  19. self.verticalStackedWidget.setGeometry(QtCore.QRect(10, 40, 781, 91))
  20. self.verticalStackedWidget.setObjectName("verticalStackedWidget")
  21. # 创建列表窗口,添加条目
  22. self.leftlist = QListWidget()
  23. self.leftlist.insertItem(0, '联系方式')
  24. self.leftlist.insertItem(1, '个人信息')
  25. self.leftlist.insertItem(2, '教育程度')
  26. # 创建三个小控件
  27. self.stack1 = QWidget()
  28. self.stack2 = QWidget()
  29. self.stack3 = QWidget()
  30. self.stack1UI()
  31. self.stack2UI()
  32. self.stack3UI()
  33. self.verticalStackedWidget.addWidget(self.stack1)
  34. self.verticalStackedWidget.addWidget(self.stack2)
  35. self.verticalStackedWidget.addWidget(self.stack3)
  36. # 水平布局,添加部件到布局中
  37. HBox = QHBoxLayout()
  38. HBox.addWidget(self.leftlist)
  39. HBox.addWidget(self.verticalStackedWidget)
  40. self.centralwidget.setLayout(HBox)
  41. self.leftlist.currentRowChanged.connect(self.display)
  42. MainWindow.setCentralWidget(self.centralwidget)
  43. self.retranslateUi(MainWindow)
  44. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  45. def retranslateUi(self, MainWindow):
  46. _translate = QtCore.QCoreApplication.translate
  47. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  48. def stack1UI(self):
  49. layout = QFormLayout()
  50. layout.addRow('姓名', QLineEdit())
  51. layout.addRow('地址', QLineEdit())
  52. self.stack1.setLayout(layout)
  53. def stack2UI(self):
  54. # zhu表单布局,次水平布局
  55. layout = QFormLayout()
  56. sex = QHBoxLayout()
  57. # 水平布局添加单选按钮
  58. sex.addWidget(QRadioButton('男'))
  59. sex.addWidget(QRadioButton('女'))
  60. # 表单布局添加控件
  61. layout.addRow(QLabel('性别'), sex)
  62. layout.addRow('生日', QLineEdit())
  63. self.stack2.setLayout(layout)
  64. def stack3UI(self):
  65. # 水平布局
  66. layout = QHBoxLayout()
  67. # 添加控件到布局中
  68. layout.addWidget(QLabel('科目'))
  69. layout.addWidget(QCheckBox('物理'))
  70. layout.addWidget(QCheckBox('高数'))
  71. self.stack3.setLayout(layout)
  72. def display(self, i):
  73. # 设置当前可见的选项卡的索引
  74. print("当前下标位置:%s" % str(1))
  75. self.verticalStackedWidget.setCurrentIndex(i)
  76. if __name__ == '__main__':
  77. app = QApplication(sys.argv)
  78. MainWindow = QMainWindow()
  79. ui = Ui_MainWindow()
  80. ui.setupUi(MainWindow)
  81. MainWindow.show()
  82. sys.exit(app.exec_())

 需要向PyUIC 生成的代码,添加如下代码片段:

重点实现QStackedWidget 子控件填充和UI界面绑定

  1. # 创建列表窗口,添加条目
  2. self.leftlist = QListWidget()
  3. self.leftlist.insertItem(0, '联系方式')
  4. self.leftlist.insertItem(1, '个人信息')
  5. self.leftlist.insertItem(2, '教育程度')
  6. # 创建三个小控件
  7. self.stack1 = QWidget()
  8. self.stack2 = QWidget()
  9. self.stack3 = QWidget()
  10. self.stack1UI()
  11. self.stack2UI()
  12. self.stack3UI()
  13. self.verticalStackedWidget.addWidget(self.stack1)
  14. self.verticalStackedWidget.addWidget(self.stack2)
  15. self.verticalStackedWidget.addWidget(self.stack3)
  16. # 水平布局,添加部件到布局中
  17. HBox = QHBoxLayout()
  18. HBox.addWidget(self.leftlist)
  19. HBox.addWidget(self.verticalStackedWidget)
  20. self.centralwidget.setLayout(HBox)
  21. self.leftlist.currentRowChanged.connect(self.display)
  1. def stack1UI(self):
  2. layout = QFormLayout()
  3. layout.addRow('姓名', QLineEdit())
  4. layout.addRow('地址', QLineEdit())
  5. self.stack1.setLayout(layout)
  6. def stack2UI(self):
  7. # zhu表单布局,次水平布局
  8. layout = QFormLayout()
  9. sex = QHBoxLayout()
  10. # 水平布局添加单选按钮
  11. sex.addWidget(QRadioButton('男'))
  12. sex.addWidget(QRadioButton('女'))
  13. # 表单布局添加控件
  14. layout.addRow(QLabel('性别'), sex)
  15. layout.addRow('生日', QLineEdit())
  16. self.stack2.setLayout(layout)
  17. def stack3UI(self):
  18. # 水平布局
  19. layout = QHBoxLayout()
  20. # 添加控件到布局中
  21. layout.addWidget(QLabel('科目'))
  22. layout.addWidget(QCheckBox('物理'))
  23. layout.addWidget(QCheckBox('高数'))
  24. self.stack3.setLayout(layout)
  25. def display(self, i):
  26. # 设置当前可见的选项卡的索引
  27. print("当前下标位置:%s" % str(1))
  28. self.verticalStackedWidget.setCurrentIndex(i)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/961270
推荐阅读
相关标签
  

闽ICP备14008679号