1. 程式人生 > >pyqt5 樹節點點擊實現多窗口切換

pyqt5 樹節點點擊實現多窗口切換

obj show item brush setsize split exec print button

# coding=utf-8

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        hbox = QHBoxLayout(self)
        left = QFrame(self)
        # QFrame 控件添加StyledPanel樣式能使QFrame 控件之間的界限更加明顯
        #left.setFrameShape(QFrame.StyledPanel)
        right = QFrame(self)
        #right.setFrameShape(QFrame.StyledPanel)
        splitter1 = QSplitter(Qt.Horizontal)
        splitter1.addWidget(left)
        splitter1.setSizes([20,]) #設置分隔條位置
        splitter1.addWidget(right)
        hbox.addWidget(splitter1)
        self.setLayout(hbox)


        # 樹
        self.tree = QTreeWidget(left)
        self.tree.setStyleSheet("background-color:#eeeeee;border:outset;color:#215b63;")
        self.tree.setAutoScroll(True)
        self.tree.setEditTriggers(QAbstractItemView.DoubleClicked | QAbstractItemView.EditKeyPressed)
        self.tree.setTextElideMode(Qt.ElideMiddle)
        #self.tree.setIndentation(30)
        self.tree.setRootIsDecorated(True)
        self.tree.setUniformRowHeights(False)
        self.tree.setItemsExpandable(True)
        self.tree.setAnimated(False)
        self.tree.setHeaderHidden(True)
        self.tree.setExpandsOnDoubleClick(True)
        self.tree.setObjectName("tree")


        # 設置根節點
        root = QTreeWidgetItem(self.tree)
        root.setText(0, ‘系統管理‘)
        # 設置樹形控件的列的寬度
        #self.tree.setColumnWidth(0, 150)
        # 設置子節點1
        child1 = QTreeWidgetItem()
        child1.setText(0, ‘增加人員信息‘)
        root.addChild(child1)
        # 設置子節點2
        child2 = QTreeWidgetItem(root)
        child2.setText(0, ‘查詢人員信息‘)
        # 加載根節點的所有屬性與子控件
        self.tree.addTopLevelItem(root)
        # 設置stackedWidget
        self.stackedWidget = QStackedWidget(right)

        # 設置第一個面板
        self.form1 = QWidget()
        self.formLayout1 = QHBoxLayout(self.form1)
        self.label1 = QLabel()
        self.label1.setText("增加人員信息面板")
        self.label1.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
        self.label1.setAlignment(Qt.AlignCenter)
        self.label1.setFont(QFont("Roman times", 50, QFont.Bold))
        self.formLayout1.addWidget(self.label1)

        # 設置第二個面板
        self.form2 = QWidget()
        self.formLayout2 = QHBoxLayout(self.form2)
        self.label2 = QLabel()
        self.label2.setText("查詢人員信息面板")
        self.label2.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
        self.label2.setAlignment(Qt.AlignCenter)
        self.label2.setFont(QFont("Roman times", 50, QFont.Bold))
        self.formLayout2.addWidget(self.label2)

        # 將兩個面板,加入stackedWidget
        self.stackedWidget.addWidget(self.form1)
        self.stackedWidget.addWidget(self.form2)

        # 樹節點監聽事件
        self.tree.clicked.connect(self.onClicked)


        # 窗口最大化
        self.showMaximized()
        self.setWindowTitle(‘樹窗口分隔案列‘)
        self.show()

    def onClicked(self,qmodeLindex):
        item=self.tree.currentItem()
        print(‘Key=%s,value=%s‘%(item.text(0),item.text(1)))
        if item.text(0) == ‘增加人員信息‘:
            self.on_pushButton1_clicked()
        elif item.text(0) == ‘查詢人員信息‘:
            self.on_pushButton2_clicked()
        else:
            print(‘返回主界面‘)


    # 按鈕一:打開第一個面板
    def on_pushButton1_clicked(self):
        self.stackedWidget.setCurrentIndex(0)

    # 按鈕二:打開第二個面板
    def on_pushButton2_clicked(self):
        self.stackedWidget.setCurrentIndex(1)


if __name__ == ‘__main__‘:
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

  

pyqt5 樹節點點擊實現多窗口切換