PyQT5 (五十二) QTreeWidget 添加 修改和删除树控件中的节点 的案例

PyQT5 (五十二) QTreeWidget 添加 修改和删除树控件中的节点 的案例_第1张图片

QTreeWidget 添加 修改和删除树控件中的节点 的案例

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem, QMenu, QVBoxLayout, QMainWindow, \
    QTreeWidget, QTreeWidgetItem, QHBoxLayout, QPushButton, QInputDialog

'''
QTreeWidget 添加 修改和删除树控件中的节点 的案例

'''


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

    def initUI(self):
        # 设置定位和左上角坐标
        self.setGeometry(300, 300, 460, 360)
        # 设置窗口标题
        self.setWindowTitle('QTreeWidget 添加 修改和删除树控件中的节点 的演示')
        # 设置窗口图标
        # self.setWindowIcon(QIcon('../web.ico'))

        operatorLayout = QHBoxLayout()
        addBtn = QPushButton('添加节点')
        updateBtn = QPushButton('修改节点')
        deleteBtn = QPushButton('删除节点')
        operatorLayout.addWidget(addBtn)
        operatorLayout.addWidget(updateBtn)
        operatorLayout.addWidget(deleteBtn)
        addBtn.clicked.connect(self.addNode)
        updateBtn.clicked.connect(self.updateNode)
        deleteBtn.clicked.connect(self.deleteNode)

        self.tree = QTreeWidget()
        # 为树控件指定列数
        self.tree.setColumnCount(2)

        # 指定列标签
        self.tree.setHeaderLabels(["Key", "Value"])
        # 添加根节点
        root = QTreeWidgetItem(self.tree)
        root.setText(0, '根节点')
        root.setIcon(0, QIcon('../web.ico'))
        # self.tree.setColumnWidth(0, 160)

        # 添加子节点
        child1 = QTreeWidgetItem(root)
        child1.setText(0, '子节点1')
        child1.setText(1, '1')
        # child1.setIcon(0, QIcon('../web.ico'))
        # child1.setCheckState(0, Qt.Checked)

        # 添加子节点2
        child2 = QTreeWidgetItem(root)
        child2.setText(0, '子节点2')
        child2.setText(1, '2')
        child2.setIcon(0, QIcon('../web.ico'))

        # 为child2添加一个子节点
        child3 = QTreeWidgetItem(child2)
        child3.setText(0, '子节点3')
        child3.setText(1, '3')
        # child3.setIcon(0, QIcon('../web.ico'))
        # 展开所有节点
        self.tree.expandAll()
        self.tree.clicked.connect(self.onTreeClicked)
        # self.setCentralWidget(self.tree)

        mainLayout = QVBoxLayout(self)
        mainLayout.addLayout(operatorLayout)
        mainLayout.addWidget(self.tree)
        self.setLayout(mainLayout)

    def onTreeClicked(self,index):
        item = self.tree.currentItem()
        print(index.row())
        print('key=%s,value=%s' %(item.text(0),item.text(1)))

    def addNode(self):
        print('添加节点')
        item = self.tree.currentItem()
        print(item)
        node = QTreeWidgetItem(item)
        node.setText(0,'新节点')
        node.setText(1,'新值')

    def updateNode(self):
        print('修改节点')
        item = self.tree.currentItem()

        text, ok = QInputDialog.getText(self, '输入节点名称', '节点名称:')
        if ok and text:
            item.setText(0, text)
            text, ok = QInputDialog.getText(self, '输入节点值', '节点得值:')
            if ok and text:
                item.setText(1, text)

    def deleteNode(self):
        print('删除节点')
        item = self.tree.currentItem()
        # 固定根节点
        root = self.tree.invisibleRootItem()
        for item in self.tree.selectedItems():
            (item.parent() or root).removeChild(item)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 设置应用图标
    app.setWindowIcon(QIcon('../web.ico'))
    w = ModifyTreeDemo()
    w.show()
    sys.exit(app.exec_())

你可能感兴趣的:(pyqt5,qt,python,1024程序员节)