1. 程式人生 > >Python GUI 之 Treeview 學習

Python GUI 之 Treeview 學習

寬度 imp columns expand item 添加 pro comm tails

例子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 學習