[英]Printing and inserting selected row in treeview into tkinter entry widget
我有以下代碼可以在entry
小部件中打印和插入選定的treeview
行,但在如何執行方面遇到了挑戰。我的List profile
沒有在treeview
正確顯示內容,我希望年份出現在列指定下和月份一樣。
當我選擇該行時,它會將('end',)
打印到我的終端並在我的兩個entry
小部件中插入end
而不是選定的。
from tkinter import *
from tkinter import ttk
profile = [("2012", "JANUARY"),
("2013", "FEBRUARY"),
("2014", "MARCH"),
("2015", "APRIL"),
("2016", "MAY")
]
def content(event):
print(tree.selection()) # this will print the row inserted
for nm in tree.selection():
e1.insert(END, nm)
e2.insert(END, nm)
root = Tk()
tree = ttk.Treeview(root, columns=("columns1","columns2" ), show="headings",
selectmode="browse")
tree.heading("#1", text="YEAR")
tree.heading("#2", text="MONTH")
tree.insert("", 1, END, values=profile)
tree.bind("<<TreeviewSelect>>", content)
tree.pack()
e1 = Entry(root)
e1.pack()
e2 = Entry(root)
e2.pack()
root.mainloop()
首先,您需要遍歷您的profile
列表以填充樹視圖:
for values in profile:
tree.insert("", END, values=values)
你在做什么:
tree.insert("", 1, END, values=profile)
創建了一行,項目名稱為 'end' 並為profile
的前兩個元組賦值。
我猜您想在e1
插入所選行的年份,在e2
插入月份。 但是, tree.selection()
為您提供所選項目的名稱,而不是值,但您可以使用year, month = tree.item(nm, 'values')
獲取它們。 所以content
函數變成
def content(event):
print(tree.selection()) # this will print the names of the selected rows
for nm in tree.selection():
year, month = tree.item(nm, 'values')
e1.insert(END, year)
e2.insert(END, month)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.