堆叠窗口QStackedWidget

 

经常将QStackedWidget和QListWidget或者QListView搭配使用

 1 import sys
 2 from PyQt5.QtWidgets import QApplication, QWidget, QStackedWidget, QLabel, QLineEdit, QDateEdit, \
 3     QComboBox, QTextEdit, QListWidget, QGridLayout, QHBoxLayout
 4 
 5 class Demo(QWidget):
 6     def __init__(self):
 7         super(Demo, self).__init__()
 8         self.stack1 = QWidget() #实例化一个窗口
 9         self.stack2 = QWidget()
10         self.stack3 = QTextEdit()
11 
12         self.stack1_init()
13         self.stack2_init()
14 
15         self.stacked_widget = QStackedWidget(self)  # 创建一个堆叠窗口【虚拟窗口】
16         self.stacked_widget.addWidget(self.stack1)  #给堆叠窗口添加子窗口或子控件
17         self.stacked_widget.addWidget(self.stack2)
18         self.stacked_widget.addWidget(self.stack3)
19         self.stacked_widget.currentChanged.connect(lambda: print(self.stacked_widget.currentIndex()))
20         #堆叠窗口发生变化时,都会触发信号
21         #self.stacked_widget.currentIndex()返回堆叠窗口序列号
22 
23         self.list_widget = QListWidget(self)  #创建列表控件--用来放标签项
24         self.list_widget.addItem('Basic Info')   #在列表控件中添加标签项。参数:标签显示文本
25         self.list_widget.addItem('Contact Info')
26         self.list_widget.addItem('More Info')
27         self.list_widget.clicked.connect(self.change_func) #单击列表控件时触发信号
28 
29         self.h_layout = QHBoxLayout()
30         self.h_layout.addWidget(self.list_widget)
31         self.h_layout.addWidget(self.stacked_widget)
32 
33         self.setLayout(self.h_layout)
34 
35     def stack1_init(self):
36         name_label = QLabel('姓名:', self.stack1)
37         gender_label = QLabel('性别:', self.stack1)
38         bd_label = QLabel('出生日期:', self.stack1)
39 
40         name_line = QLineEdit(self.stack1)
41         items = ['请选择性别', '', '']
42         gender_combo = QComboBox(self.stack1)
43         gender_combo.addItems(items)
44         bd_dateedit = QDateEdit(self.stack1)
45 
46         g_layout = QGridLayout()
47         g_layout.addWidget(name_label, 0, 0, 1, 1)
48         g_layout.addWidget(name_line, 0, 1, 1, 1)
49         g_layout.addWidget(gender_label, 2, 0, 1, 1)
50         g_layout.addWidget(gender_combo, 2, 1, 1, 1)
51         g_layout.addWidget(bd_label, 3, 0, 1, 1)
52         g_layout.addWidget(bd_dateedit, 3, 1, 1, 1)
53 
54         self.stack1.setLayout(g_layout)
55 
56     def stack2_init(self):
57         tel_label = QLabel('Tel:', self.stack2)
58         mobile_label = QLabel('Mobile:', self.stack2)
59         add_label = QLabel('Address:', self.stack2)
60 
61         tel_line = QLineEdit(self.stack2)
62         mobile_line = QLineEdit(self.stack2)
63         add_line = QLineEdit(self.stack2)
64 
65         g_layout = QGridLayout()
66         g_layout.addWidget(tel_label, 0, 0, 1, 1)
67         g_layout.addWidget(tel_line, 0, 1, 1, 1)
68         g_layout.addWidget(mobile_label, 1, 0, 1, 1)
69         g_layout.addWidget(mobile_line, 1, 1, 1, 1)
70         g_layout.addWidget(add_label, 2, 0, 1, 1)
71         g_layout.addWidget(add_line, 2, 1, 1, 1)
72 
73         self.stack2.setLayout(g_layout)
74 
75     def change_func(self):
76         self.stacked_widget.setCurrentIndex(self.list_widget.currentIndex().row())
77         #设置堆叠窗口显示指定索引的窗口
78         #self.list_widget.currentIndex().row() 返回当前列表选中项的索引号
79 
80 if __name__ == '__main__':
81     app = QApplication(sys.argv)
82     demo = Demo()
83     demo.show()
84     sys.exit(app.exec_())

 

样式:

 

posted @ 2019-02-17 20:16  天子骄龙  阅读(2980)  评论(0编辑  收藏  举报