Header Ads

2017年4月26日 星期三

Python | python tkinter treeview 多欄位 新增 刪除 經驗分享

本來想要使用listbox做多欄位的表格
上網查了之後
發現大家用treeview來呈現
我有加上新增、刪除的button😊

用的是python 3.5
import tkinter as tk 
from tkinter import ttk

root = tk.Tk()

tree = ttk.Treeview(root)

tree["columns"]=("one","two")
tree.column("one", width=100 )
tree.column("two", width=100)
tree.heading("one", text="coulmn A")
tree.heading("two", text="column B")

tree.insert("" , 0, text="Line 1", values=("1A","1b"))

apple = tree.insert("", 1, "iamD", text="DDDDD")
tree.insert(apple, "end","iamd", text="ddddd", values=("2A","2B"))

##alternatively:
tree.insert("", 2, "dir3", text="test")
tree.insert("dir3", "end", text=" kkkkk",values=("3A"," 3B"))

tree.pack()
e=tk.Entry(root,show=None)  
e.pack()

e1=tk.Entry(root,show=None)  
e1.pack()

e2=tk.Entry(root,show=None)  
e2.pack()

def add():
        var=e.get()
        var1=e1.get()
        var2=e2.get()
        tree.insert("","end",text=var,values=(var1,var2))
        e.delete(0,'end')
        e1.delete(0,'end')
        e2.delete(0,'end')

b1=tk.Button(root,text='add',width=15,command=add)
b1.pack()

def delete():
        selected_item = tree.selection()[0] ## get selected item
        tree.delete(selected_item)

b2=tk.Button(root,text='delete',width=15,command=delete)
b2.pack()
root.mainloop()

沒有留言:

張貼留言