Python GUI 之 Treeview 学习,,例子1from tk


例子1

from tkinter import *
import tkinter.ttk as ttk

win = Tk()
win.title("Treeview 学习")

col = [1,2,3,4]
data = {"item0":["1a","2a","3a","4a"], "item1":{"num0":["1n", "2n", "3n"," 4n"],"num1":["1m","2m","3m","4m"]}, "item2":["1c","2c","3c","4c"]}

tree = ttk.Treeview(win, columns = col, height = 10, show = "tree")
#show = "tree", 第一列也会被显示出来
#也可用show = "headings" 把第一列隐藏起来
#height 的单位是字符,本例里可以显示10行

tree.column(‘0‘,width=150,anchor=‘center‘) #指定第一列的宽度和名称, 如果show = "headings", 这一列就被隐藏。
tree.column(‘1‘,width=100,anchor=‘center‘)
tree.column(‘2‘,width=100,anchor=‘w‘)
tree.column(‘3‘,width=100,anchor=‘center‘)
tree.column(‘4‘,width=100,anchor=‘center‘)
tree.heading(‘0‘,text=‘column0‘)
tree.heading(‘1‘,text=‘column1‘)
tree.heading(‘2‘,text=‘column2‘)
tree.heading(‘3‘,text=‘column3‘)
tree.heading(‘4‘,text=‘column4‘)

tree.insert(‘‘,‘end‘,values= data["item0"])
tree.insert(‘‘,‘end‘,values= data["item2"])

tree.pack()

win.mainloop()
代码运行结果:

例子2, subtrree

from tkinter import *
import tkinter.ttk as ttk

win = Tk()
win.title("Treeview 学习")

col = [1,2,3,4]
data = {"item0":["1a","2a","3a","4a"], "item1":{"num0":["1n", "2n", "3n"," 4n"],"num1":["1m","2m","3m","4m"]}, "item2":["1c","2c","3c","4c"]}

tree = ttk.Treeview(win, columns = col, height = 10, show = "tree")
#show = "tree", 第一列也会被显示出来
#也可用show = "headings" 把第一列隐藏起来
#height 的单位是字符,本例里可以显示10行

tree.column(‘0‘,width=www.hjylp178.com 150,anchor=‘center‘) #指定第一列的宽度和名称, 如果show = "headings", 这一列就被隐藏。
tree.column(‘1‘,width=www.dfgjyl.cn 100,anchor=‘center‘)
tree.column(‘2‘,width=www.yongxinzaixian.cn100,anchor=‘w‘)
tree.column(‘3‘,width=100,anchor=‘center‘)
tree.column(‘4‘,width=100,anchor=‘center‘)
tree.heading(‘0‘www.gangchengyuLe178.com,text=‘column0‘)
tree.heading(‘1‘,text=www.ysyl157.com ‘column1‘)
tree.heading(‘2‘,text=www.mcyllpt.com ‘column2‘)
tree.heading(‘3‘,text=www.meiwanyule.cn ‘column3‘)
tree.heading(‘4‘,text=‘column4‘)

#用递归法遍历带子字典或列表的数据
def process_dict(d, tree, tr):
for k,v in d.items():
if type(v) == list:
if type(v[0]) == dict:
trr www.yigouyule2.cn= tree.insert(tr, ‘end‘, text=k, open=True)
for ls in v:
process_dict(ls, tree, trr)
else:
tree.insert(tr, ‘end‘, text=k, values= v)
elif type(v) == dict:
trr = tree.insert(tr, ‘end‘, text=k, open = True)
process_dict(v, tree, trr)
process_dict(data,tree, "")

tree.pack()

win.mainloop()
代码运行结果:

例子3, 添加滚动条

from tkinter import *
import tkinter.ttk as ttk

win = Tk()
win.geometry("100x100")
win.title("Treeview 学习")

col = [1,2,3,4]
data = {"item0":["1a","2a","3a","4a"], \
"item1":{"num0":["1n", "2n", "3n"," 4n"],"num1":["1m","2m","3m","4m"]},
"item2":["1c","2c","3c","4c"], \
"item3": ["1a", "2a", "3a", "4a"], \
"item4": {"num40": ["1n", "2n", "3n", " 4n"], "num41": ["1m", "2m", "3m", "4m"]},
"item6": ["1c", "2c", "3c", "4c"],\
"item7":["1a","2a","3a","4a"], \
"item8":{"num80":["1n", "2n", "3n"," 4n"],"num81":["1m","2m","3m","4m"]},
"item9":["1c","2c","3c","4c"],\
"item10":["1a","2a","3a","4a"], \
"item11":{"num110":["1n", "2n", "3n"," 4n"],"num111":["1m","2m","3m","4m"]},
"item12":["1c","2c","3c","4c"]
}

tree = ttk.Treeview(win, columns = col, height = 10, show = "tree")
#show = "tree", 第一列也会被显示出来
#也可用show = "headings" 把第一列隐藏起来
#height 的单位是字符,本例里可以显示10行

tree.column(‘0‘,width=150,anchor=‘center‘) #指定第一列的宽度和名称, 如果show = "headings", 这一列就被隐藏。
tree.column(‘1‘,width=100,anchor=‘center‘)
tree.column(‘2‘,width=100,anchor=‘w‘)
tree.column(‘3‘,width=100,anchor=‘center‘)
tree.column(‘4‘,width=100,anchor=‘center‘)
tree.heading(‘0‘,text=‘column0‘)
tree.heading(‘1‘,text=‘column1‘)
tree.heading(‘2‘,text=‘column2‘)
tree.heading(‘3‘,text=‘column3‘)
tree.heading(‘4‘,text=‘column4‘)

#用递归法遍历带子字典或列表的数据
def process_dict(d, tree, tr):
for k,v in d.items():
if type(v) == list:
if type(v[0]) == dict:
trr = tree.insert(tr, ‘end‘, text=k, open=True)
for ls in v:
process_dict(ls, tree, trr)
else:
tree.insert(tr, ‘end‘, text=k, values= v)
elif type(v) == dict:
trr = tree.insert(tr, ‘end‘, text=k, open = True)
process_dict(v, tree, trr)
process_dict(data,tree, "")

#y滚动条
yscrollbar = Scrollbar(win, orient=VERTICAL, command=tree.yview)
tree.configure(yscrollcommand = yscrollbar.set)
yscrollbar.pack(side = RIGHT, fill = Y)
#x滚动条
xscroll = Scrollbar(win, orient=HORIZONTAL, command=tree.xview)
tree.configure(xscrollcommand = xscroll.set)
xscroll.pack(side = BOTTOM, fill = X)

tree.pack(side = TOP, expand = 1, fill = BOTH)

win.mainloop()
代码运行结果:
---------------------
作者:weixin_41501380
来源:CSDN
原文:https://blog.csdn.net/weixin_41501380/article/details/83933484
版权声明:本文为博主原创文章,转载请附上博文链接!

Python GUI 之 Treeview 学习

评论关闭