iT邦幫忙

2021 iThome 鐵人賽

DAY 28
1
自我挑戰組

一起用python寫UI系列 第 28

Day28 用python寫UI-聊聊Treeview(二)

今天的範例也有結合昨天的程式碼,要聊聊直接在介面上做增減,等不及的話就趕快往下滑吧~

♠♣今天的文章大綱♥♦

  • 刪除項目
  • 插入項目
  • 綁定卷軸

刪除項目

import tkinter as tk
import tkinter.ttk as ttk

def remove():
    des = tree.selection()
    for de in des:
        tree.delete(de)

root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('500x200')

tree=ttk.Treeview(root,columns=("節日"))
tree.heading("#0",text="節日")
tree.heading("#1",text="日期")

tree.insert("",index="end",text="國慶日",values="10/10")
tree.insert("",index="end",text="聖誕節",values="12/25")
tree.insert("",index="end",text="元旦",values="1/1")
tree.insert("",index="end",text="愚人節",values="4/1")
tree.insert("",index="end",text="兒童節",values="4/4")

btn=tk.Button(root,text="remove",command=remove)
btn.pack(pady=3)

tree.pack()
root.mainloop()

執行結果⬇⬇⬇
https://ithelp.ithome.com.tw/upload/images/20211011/20140047Wzblh2opwk.png
點選要刪除的資料,按下remove按鍵。
https://ithelp.ithome.com.tw/upload/images/20211011/20140047wFcC3ICJ9n.png


插入項目

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('800x300')

def remove():
    des = tree.selection()
    for de in des:
        tree.delete(de)
def insert():
    date=dateEntry.get()
    festival=festival.get()
    if(len(date.strip())==0 or len(city.strip())==0):
        return
    tree.insert("","end",text=date,values=(festival))
    dateEntry.delete(0,"end")
    festivalEntry.delete(0,"end")

root.rowconfigure(1,weight=1)
root.columnconfigure(1,weight=1)
root.columnconfigure(3,weight=1)

dateL=tk.Label(root,text="Festival",bg="#7AFEC6",fg="#4A4AFF",font=("Harlow Solid Italic",10,"bold"))
dateL.grid(row=0,column=0,padx=5,pady=3,sticky="w")
dateEntry=tk.Entry()
dateEntry.grid(row=0,column=1,padx=5,pady=3,sticky="w"+"e")
festivalL=tk.Label(root,text=" Date",bg="#7AFEC6",fg="#4A4AFF",font=("Harlow Solid Italic",10,"bold"))
festivalL.grid(row=0,column=2,padx=5,pady=3,sticky="e")
festivalEntry=tk.Entry()
festivalEntry.grid(row=0,column=3,padx=5,pady=3,sticky="w"+"e")

tree=ttk.Treeview(root,columns=("節日"))
tree.heading("#0",text="節日")
tree.heading("#1",text="日期")

tree.insert("",index="end",text="國慶日",values="10/10")
tree.insert("",index="end",text="聖誕節",values="12/25")
tree.insert("",index="end",text="元旦",values="1/1")
tree.insert("",index="end",text="愚人節",values="4/1")
tree.insert("",index="end",text="兒童節",values="4/4")

btn=tk.Button(root,text="remove",command=remove)
btn.grid(row=2,column=2,padx=5,pady=3,sticky="w")
ibtn=tk.Button(root,text="insert",command=insert)
ibtn.grid(row=0,column=4,padx=5,pady=3,sticky="w")
tree.grid(row=1,column=0,columnspan=5,padx=5,pady=3,sticky="w"+"e"+"n"+"s")

root.mainloop()

執行結果⬇⬇⬇
https://ithelp.ithome.com.tw/upload/images/20211011/201400478keajHmFkT.png


綁定卷軸

結合昨天的階層做範例

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('500x100')

Nov={"國慶日":"10/10"}
Dec={"聖誕節":"12/25"}
Jen={"元旦":"1/1"}
Mar={"生日":"3/29"}
Apr={"愚人節":"4/1","兒童節":"4/4"}

tree=ttk.Treeview(root, columns=("festival"))

yscrollbar=tk.Scrollbar(root)  #卷軸
yscrollbar.pack(side="right",fill='y')
yscrollbar.config(command=tree.yview)
tree.configure(yscrollcommand=yscrollbar.set)
tree.column("festival",anchor="center")

tree.heading ("#0",text="節日")
tree.heading("festival", text="日期")
idNov = tree.insert("", index="end", text="Nov")
idDec = tree.insert("", index="end", text="Dec")
idJen = tree.insert("", index="end", text="Jen")
idMar = tree.insert("", index="end", text="Mar")
idApr = tree.insert("", index="end", text="Apr")

for festival in Nov.keys():
    tree.insert(idNov, index="end", text=festival, values=Nov[festival])
for festival in Dec.keys():
    tree.insert(idDec, index="end", text=festival, values=Dec[festival])
for festival in Jen.keys():
    tree.insert(idJen, index="end", text=festival, values=Jen[festival])
for festival in Mar.keys():
    tree.insert(idMar, index="end", text=festival, values=Mar[festival])
for festival in Apr.keys():
    tree.insert(idApr, index="end", text=festival, values=Apr[festival])

tree.pack()
root.mainloop()

執行結果⬇⬇⬇
https://ithelp.ithome.com.tw/upload/images/20211011/20140047QVanEWfhlV.png
往下捲
https://ithelp.ithome.com.tw/upload/images/20211011/201400478uZYewtUv1.png


我們已經剩下最後一個主題了呢,時間過得好快,明後天會是一個大主題,超有趣的喔~
這邊先不輸是啥主題,明天就跟你們說啦~~~???
/images/emoticon/emoticon39.gif


上一篇
Day27 用python寫UI-聊聊Treeview(一)
下一篇
Day29 用python寫UI-聊聊Canvas(一)
系列文
一起用python寫UI30

尚未有邦友留言

立即登入留言