以下是采用pyqt做的一个综合页面布局示例:
# -*- coding: utf-8 -*- from PyQt4.QtGui import * from PyQt4.QtCore import * import sys class Stacked(QDialog): def __init__(self, parent=None): super(Stacked, self).__init__(parent) self.setWindowTitle(self.tr("StackedWidget")) self.setGeometry(300,300,800,600) #stack1======================================== self.leftlist = QListWidget() self.leftlist.insertItem(0, 'window1') self.leftlist.insertItem(1, 'window2') self.leftlist.insertItem(2, 'window3') #======================tab页设计================== Tab = QTabWidget() tab1 = QWidget() tab2 = QWidget() tab3 = QWidget() Tab.addTab(tab1, 'tab1') Tab.addTab(tab2, 'tab2') Tab.addTab(tab3, 'tab3') tab1_btn = QPushButton("tab1") tab2_btn = QPushButton("tab2") tab3_btn = QPushButton("tab3") tab1_layout = QHBoxLayout() tab1_layout.addWidget(tab1_btn) tab1.setLayout(tab1_layout) tab2_layout = QHBoxLayout() tab2_layout.addWidget(tab2_btn) tab2.setLayout(tab2_layout) tab3_layout = QHBoxLayout() tab3_layout.addWidget(tab3_btn) tab3.setLayout(tab3_layout) #======================tab完================== lab2 = QLabel(u'界面1-2 ') lab3 = QLabel(u'界面1-3 ') self.stack = QStackedWidget() self.stack.addWidget(Tab) self.stack.addWidget(lab2) self.stack.addWidget(lab3) QObject.connect(self.leftlist, SIGNAL('currentRowChanged(int)'), self.stack, SLOT('setCurrentIndex(int)')) #stack1======================================== #stack2======================================== self.leftlist2 = QListWidget() self.leftlist2.insertItem(0, 'window21') self.leftlist2.insertItem(1, 'window22') self.leftlist2.insertItem(2, 'window23') lab21 = QLabel(u'界面2-1 ') lab22 = QLabel(u'界面2-2 ') lab23 = QLabel(u'界面2-3 ') self.stack2 = QStackedWidget() self.stack2.addWidget(lab21) self.stack2.addWidget(lab22) self.stack2.addWidget(lab23) QObject.connect(self.leftlist2, SIGNAL('currentRowChanged(int)'), self.stack2, SLOT('setCurrentIndex(int)')) #stack2======================================== #stack3======================================== self.leftlist3 = QListWidget() self.leftlist3.insertItem(0, 'window31') self.leftlist3.insertItem(1, 'window32') self.leftlist3.insertItem(2, 'window33') lab31 = QLabel(u'界面3-1 ') lab32 = QLabel(u'界面3-2 ') lab33 = QLabel(u'界面3-3 ') self.stack3 = QStackedWidget() self.stack3.addWidget(lab31) self.stack3.addWidget(lab32) self.stack3.addWidget(lab33) QObject.connect(self.leftlist3, SIGNAL('currentRowChanged(int)'), self.stack3, SLOT('setCurrentIndex(int)')) #stack3======================================== self.leftlist2.hide() self.leftlist3.hide() self.stack2.hide() self.stack3.hide() #===============================界面按钮设计&组装===================== self.btn = QPushButton(u"界面1") self.btn.clicked.connect(self.surface1) self.btn2 = QPushButton(u"界面2") self.btn2.clicked.connect(self.surface2) self.btn3 = QPushButton(u"界面3") self.btn3.clicked.connect(self.surface3) btn_layout = QHBoxLayout() btn_layout.addWidget(self.btn) btn_layout.addWidget(self.btn2) btn_layout.addWidget(self.btn3) #===================stack&list 组装================== vboxlayout = QVBoxLayout() vboxlayout2 = QVBoxLayout() vboxlayout.addWidget(self.leftlist) vboxlayout.addWidget(self.leftlist2) vboxlayout.addWidget(self.leftlist3) vboxlayout2.addWidget(self.stack) vboxlayout2.addWidget(self.stack2) vboxlayout2.addWidget(self.stack3) stack_list = QHBoxLayout() stack_list.setMargin(5) # 对话框边距设为5 Margin 边距 5px stack_list.addLayout(vboxlayout) stack_list.addLayout(vboxlayout2) stack_list.setStretchFactor(vboxlayout, 1) stack_list.setStretchFactor(vboxlayout2, 3) # 设定了list与stack比例为1:3。 mainlayout = QVBoxLayout() mainlayout.addLayout(btn_layout) mainlayout.addLayout(stack_list) self.setLayout(mainlayout) def surface1(self): self.leftlist.setVisible(True) self.stack.setVisible(True) self.leftlist2.hide() self.leftlist3.hide() self.stack2.hide() self.stack3.hide() def surface2(self): self.leftlist2.setVisible(True) self.stack2.setVisible(True) self.leftlist.hide() self.leftlist3.hide() self.stack.hide() self.stack3.hide() def surface3(self): self.leftlist3.setVisible(True) self.stack3.setVisible(True) self.leftlist.hide() self.leftlist2.hide() self.stack.hide() self.stack2.hide() if __name__ == '__main__': app = QApplication(sys.argv) main = Stacked() main.show() app.exec_()
效果:
行万里路,不忘初心!