Python GUI编程网 > PyQt5高级控件 > QTreeWidget树控件
python教程

QTreeWidget树控件

发布日期:2021年4月24日 22:57
阅读:2092
作者:Python GUI编程网

QTreeWidget树控件

00:00 / 01:23
1x
2x
1.5x
1.25x
1x
0.8x
0.5x

QTreeWidget树控件

################################
# PyQt5中文网 - PyQt5全套视频教程 #
#    https://www.PyQt5.cn/     #
#         主讲: 村长            #
################################

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_())