python ui - tkinter

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, "") #json 窗口 node
 
#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.x) #鼠标点击处的列号, 以#开头, 如#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("<ButtonRelease-1>", popup)
 
win.mainloop()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值