当前位置: 首页 PyQt5高级控件 - 表格与树控件 QTreeWidget树控件
add-vip

QTreeWidget树控件

一、学习指导

和上面我们学习的表格控件一样,QTreeWidget树控件会作为我们的重点进行一些详细的讲解。本节课中主要从QTreeWidget树控件的继承结构方面来了解树控件的具体用法,同时也给大家介绍几种常用的属性。

二、代码演示

from PyQt5.Qt import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("高级控件-QTreeWidget树控件 - PyQt5中文网")
        self.resize(600, 500)
        self.func_list()

    def func_list(self):
        self.func()

    def func(self):
        tree = QTreeWidget(self)
        tree.setFixedSize(self.width(), self.height())  # 设置控件尺寸
        tree.setColumnCount(4)
        tree.setHeaderLabels(['文件类型', '文件大小', '创建时间'])
        tree.setColumnWidth(0, 120)

        # 创建跟节点
        root1 = QTreeWidgetItem(tree)
        root1.setText(0, '文件下载')
        root1.setIcon(0, QIcon('文件夹.png'))

        child1 = QTreeWidgetItem(root1)
        child1.setText(0, 'TXT文件')
        child1.setText(1, '300MB')
        child1.setText(2, '2020年')
        child1.setText(3, '创建')
        child1.setIcon(0, QIcon('文件.png'))

        # 默认展开
        tree.expandAll()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()

    window.show()
    sys.exit(app.exec_())


相关文章