其他分享
首页 > 其他分享> > PyQt5基础学习-QTreeWidget(树控件) 1.QTreeWidget().setColumnCount(设置列数) 2.QTreeWidget().setHeaderLabels(设置列标

PyQt5基础学习-QTreeWidget(树控件) 1.QTreeWidget().setColumnCount(设置列数) 2.QTreeWidget().setHeaderLabels(设置列标

作者:互联网

主要介绍树的基本用法,为树添加子节点和为子节点添加更小的子节点,同时展开所有的树节点

BasicTreeWidget.py 

"""
树控件(QTreeWidget)的基本用法
"""

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

class BasicTreeWidget(QMainWindow):

    def __init__(self):
        super(BasicTreeWidget, self).__init__()
        self.resize(500, 300)
        self.setWindowTitle("树控件(QTreeWidget)的基础用法")

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

        #指定列标签
        self.tree.setHeaderLabels(['Key', 'Value'])
        #将self.tree作为根节点
        root = QTreeWidgetItem(self.tree)
        root.setText(0, "根")
        root.setIcon(0, QIcon("picture/F6fhJr.jpg"))
        #设置列宽
        self.tree.setColumnWidth(0, 250)

        #添加子节点1
        child1 = QTreeWidgetItem(root)
        child1.setText(0, "子节点1")
        child1.setText(1, "子节点1的数据")
        child1.setIcon(0, QIcon("picture/F6fhJr.jpg"))
        #可以进行勾选
        child1.setCheckState(0, Qt.Checked)

        #添加子节点2
        child2 = QTreeWidgetItem(root)
        child2.setText(0, "子节点2")
        child2.setIcon(0, QIcon("picture/F6fhJr.jpg"))

        #为child2添加一个子节点
        child3 = QTreeWidgetItem(child2)
        child3.setText(0, "子节点2-1")
        child3.setText(1, "新的值")
        child3.setIcon(0, QIcon("picture/F6fhJr.jpg"))

        self.tree.expandAll()
        self.setCentralWidget(self.tree)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = BasicTreeWidget()
    main.show()

    sys.exit(app.exec_())

 

标签:__,控件,QTreeWidget,self,tree,setText,设置,节点
来源: https://www.cnblogs.com/hyq-lst/p/15864221.html