tkinter treeview用法

创建 Treeview 使用 ttk.Treeview 类,语法如下:

tree = ttk.Treeview(master, options) 

nw n ne
w center e
sw s se

一、Treeview参数

参数

描述

代码实例

master

父容器

columns

值为一个列表(tuple),列表中的每个元素都代表表格中的一列

tree["columns"] = ("姓名", "年龄", "性别")

displaycolumns

指定显示哪些数据列以及它们出现的顺序,或者"#all"。

displaycolumns=[2, 3, 1]

显示的顺序为:年龄 性别 姓名

height

表格显示的行数

height=10

padding

内容距离组件边缘的距离(左,上,右,下)

padding=[20,10,20,10]

selectmode

“extended”:(默认值)可以选择多个(行)项目(用Ctrl+鼠标)

“browse”:每次只会选中一行。

“none”:不更改选择

selectmode=“extended” 

show

"tree headings":表示显示所有列,默认值(列表模式)

“tree”:表示仅显示第一列(树模式)

“headings”:表示显示除一列的其他列(列表模式)

注意:‘#0’(第一列)是永远存在的

show="headings"

xscrollcommand

X轴滚动条绑定

yscrollcommand

Y轴滚动条绑定

style

样式

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

style = ttk.Style()
style.configure("Custom.Treeview.Heading", arrow="custom_arrow")

data = {'姓名': 80, '年龄': 80, '性别': 100}
info = [('张三', 25, '男'), ('李四', 30, '男'), ('王五', 18, '女'), ('王一', 20, '女'), ('王二', 22, '女'), ('王三', 28, '女')]

xscroll = ttk.Scrollbar(root, orient='horizontal')  # x轴滚动条
yscroll = ttk.Scrollbar(root, orient='vertical')  # y轴滚动条
tree = ttk.Treeview(root, style="Custom.Treeview", height=5, padding=[10, 10, 10, 10], show='headings',
                    columns=list(data),selectmode="extended",xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
xscroll.config(command=tree.xview)  # table绑定x轴滚动条事件
yscroll.config(command=tree.yview)  # table绑定y轴滚动条事件
tree.grid(row=0, column=0, sticky='nsew')
xscroll.grid(row=1, column=0, sticky='nsew')
yscroll.grid(row=0, column=1, sticky='nsew')

for i in data:
    tree.heading(i, text=i)
    tree.column(i, width=data[i], anchor='center', stretch=False)  # stretch表示调整大小时是否调整列的宽度

for x in info:
    tree.insert("", 'end', value=x)

root.mainloop()

二、item参数

参数 描述
text 树状结构这边的名称。
image 树状结构这边的名称的左边加个图。
values 列表结构这边每一行的值,values未赋值的列会为空值,超过列的长度会被截断。
open 布尔值,代表子item的显示打开或关闭,open=True 显示,open=False隐藏
tags 与item关联的标记(foreground-前景色,background-背景色,font-字体,image-图片)

三、treeview常用方法

方法 描述
insert(parent, index, iid=None, **kw)
参数 描述
parent 指定父节点,如果需要插入根节点,传入""
index 指定插入位置,0表示在头部插入,END表示在尾部插入。
text 指定在图标栏展示的内容,也就是在编号"#0"列展示的内容,只有 show 参数包含 tree 时才能看到。
values 指定在数据列展示的内容,也就是 columns 指定的列。
image 指定图标栏显示的图标,是一个PhotoImage对象

heading(column, option=None, **kw)
参数

描述

column 列标识符
option 如果不设置则返回所有属性的字典,如果设置则返回那个属性的值。
text 列头名
image 列头名右的图像
anchor 文字在heading里的对齐方式,标准的tk的anchor属性
command 点击列头的回调函数

column(column, option=None, **kw)
参数 描述
column 列标识符
option 如果不设置则返回所有属性的字典,如果设置则返回那个属性的值。
id 只读属性,返回列名。
anchor 文字在cell里的对齐方式,标准的tk的anchor属性
minwidth 值,单位是像素,列的最小宽度
stretch 布尔值,表示列的宽度是否随整个部件的改动而变化。
width 列宽,单位是像素。

提示:如果要设置树状结构那列,用column=“#0”

get_children(item=None) 返回一个 item 的所有子 item,这个子 item 是一个列表形式,如果 item 没指定,则返回根目录的 item
item(item, option=None, **kw) 查询或修改指定 item 的选项
set_children(item, *newchildren) 设置一个 item 的新子 item。这里设置了之后实际是全部替换。一般用于树形结构中。
bbox(item,column=None) 返回指定item(指定单元格)的边界框(x,y,width,height)
delete(*items) 删除所有指定items及其子item,根目录item不能删除
detach(*items) 方法与delete类似,detach不是真正的删除了指定的item,而是隐藏了内容,可以使用move的方法重新将隐藏的数据再显示出来
identify_row(y) 返回位置为y(行)的item的ID
identify_column(x) 返回位置为x(列)的item的ID
identify(component, x, y) 返回在坐标(x,y)处的部件信息。部件包括:region(heading,cell等), item, column, row, 和 element。
identify_element(x, y) 返回在(x,y)处的元素。
identify_region(x, y) 返回坐标(x,y)处的Tree view组成部分
exists(item) 判断指定的item是否存在,存在返回值为True,不存在返回False。
index(item) 返回数字化的item索引,从0开始
focus(item=None) 获得选定item的iid,或者选中指定item。
move(item, parent, index) move()方法有两种作用:
(1)将detach的item重新显示(reattach)
(2)移动item指定的节点到parent的子节点中,位置由index指定
next(item) 返回item的下一个节点
prev(item) 返回item的前一个节点
parent(item) 返回item的父节点
see(item) 保证item指定的节点在Treeview控件中可见
selection(items=None) 返回当前选定的节点的iid
selection_set(*items) 选中items指定的节点
sele
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值