Python GUI 之 Treeview 学习

Python的Treeview手册  https://docs.python.org/3.1/library/tkinter.ttk.html?highlight=treeview#tkinter.ttk.Treeview

一个不错的博客文章 https://blog.csdn.net/sinat_27382047/article/details/80161637

按列内的值进行排序

https://stackoverflow.com/questions/1966929/tk-treeview-column-sort

https://stackoverflow.com/questions/22032152/python-ttk-treeview-sort-numbers/46712710

例子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()

代码运行结果:

Python GUI 之 Treeview 学习_第1张图片

例子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=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, "")

tree.pack()

win.mainloop()

代码运行结果:

Python GUI 之 Treeview 学习_第2张图片

例子3, 添加滚动条

由于bug,一旦横向拖动到最右端后又,横向滚动条会失效。

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

代码运行结果:

Python GUI 之 Treeview 学习_第3张图片

例子4, 取得行号,列号,行内的值。

from tkinter import *
import tkinter.ttk as ttk

win = Tk()
win.geometry("500x200")
win.title("Treeview 学习")

col = [1,2,3,4]
students = {"name":["kevin","Mike","Fred","Lily"], \
            "性别":["Male","Male","Male","Female"],\
            "年龄":[12,14,13,12], \
            "身高":[160,170,165,168], \
            "服装":{"帽子":[23,24,26,30],\
                  "衣服":["X","XL","XS","M"],\
                  "裤子":[29,30,31,28]}, \
            "skill":["Singing","Dancing","Piano","Dancing"]}

tree = ttk.Treeview(win, columns = col, height = 10, show = "tree headings", selectmode = "extended")
#show = "tree", 第一列也会被显示出来
#也可用show = "headings" 把第一列隐藏起来
#height 的单位是字符,本例里可以显示10行
#selectmode = "extended",  可结合ctrl键选多行
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.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')


#用递归法遍历带子字典或列表的数据
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(students,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)

def popup(event):
    "鼠标事件"
    iidy = tree.identify_row(event.y)    #行号,也叫item, 以字母I开头,第一行是I001
                                         #也可用event.x,结果是一样的。
    iidx = tree.identify_column(event.y) #鼠标点击处的列号, 以#开头, 如#0
    print("row = ", iidy)
    print("column = ", iidx)
    print("the items has been selected = ",tree.selection())              #取得被选中的行号
    item = tree.selection()[0]
    print("item = ", item)
    print("you clicked on item",iidy, ", it's text = ", tree.item(item, "text"))
    print("you clicked on item",iidy, ", it's values = ", tree.item(item, "values"))
    print("tree.get_children('') = ",tree.get_children(''))         #取得所有root下的item
    print("tree.get_children('I005') = ",tree.get_children("I005")) #取得子类I005下的item

tree.bind("", popup)

win.mainloop()

代码运行结果:

Python GUI 之 Treeview 学习_第4张图片

Python GUI 之 Treeview 学习_第5张图片

 

你可能感兴趣的:(python)