过去,我采用列表框值并将其输入到条目小部件中。现在,我需要对一棵树执行相同的操作,但是发现的唯一帮助与我想要执行的帮助类似:Printing and inserting selected row in treeview into tkinter entry widget对我不起作用。基本上,我需要弄清楚如何修改通常使用列表框的功能以用于树。
def dynamicPO(self):
sql=("""SELECT(cast(Notes as nvarchar(max))) 'PO Number', POPY.AccountCode, POPY.Vendor, Items,FORMAT(([POAmount]),'C0') As 'PO AMOUNT', FORMAT(SUM([DailyCosts].DailyCost),'C0') AS 'COSTS TO DATE',FORMAT(POAmount - SUM(DailyCosts.DailyCost), 'C0') 'Remaining Amount' FROM DailyCosts JOIN POPY ON DailyCosts.WellID = POPY.WellID AND DailyCosts.JobID = POPY.JobID AND (cast(DailyCosts.Notes as nvarchar(max)))= POPY.PONumber WHERE DailyCosts.WellID = ? and DailyCosts.JobID = ? GROUP BY (cast(DailyCosts.Notes as nvarchar(max))),POAmount,POPY.Vendor,POPY.Items,POPY.AccountCode, POPY.WellID, POPY.JobID ORDER BY POPY.Vendor""")
self.cursor.execute(sql,[self.powellid_bx.get(),self.pojobid_bx.get(ACTIVE)])
rows = self.cursor.fetchall()
for row in rows:
print(row) # it print all records in the database
self.tree.insert("", tk.END, values=row)
self.tree= ttk.Treeview(self.tab4, column=("column1", "column2", "column3","column4", "column5", "column6", "column7"), show='headings')
self.tree.grid(row=7, column = 0, columnspan =8, sticky = "NSEW")
self.tree.heading("#1", text="PO Number")
self.tree.heading("#2", text="Budget Code")
self.tree.heading("#3", text="Vendor")
self.tree.heading("#4", text="Items")
self.tree.heading("#5", text="PO Amount")
self.tree.heading("#6", text="Cost to Date")
self.tree.heading("#7", text="Remaining PO Amount")
self.tree.bind("<<TreeViewSelect>>", self.get_selected_PO)
def get_selected_PO(self,event):
global selected_tuple
if self.tree.curselection():
index = self.tree.curselection()[0]
selected_tuple = self.tree.get(index)
self.treepo_entry.insert('end', str(selected_tuple[0]))
self.treevend_entry.insert('end', selected_tuple[1])
self.treeitems_entry.insert('end', selected_tuple[2])
self.treeamount_entry.insert('end', selected_tuple[3])
self.treedate_entry.insert('end', selected_tuple[4])
self.treecomments_entry.insert('end' , selected_tuple[5])
def get_selected_PO(self,event):
for item in self.tree.selection():
self.treepo_entry.insert(END,'row')
根据文档,selection
方法将为您返回所选元素的标识符列表。即使您仅支持一个选择,它也会是一个列表。
也根据文档,您可以使用item
方法以字典的形式获取项目的所有信息。
最后,当用户选择项目时生成的事件为<<TreeviewSelect>>
。请注意,v
是小写。
这里是一个完整的示例:
import tkinter as tk
from tkinter import ttk
class Example:
def __init__(self):
root = tk.Tk()
self.tree = ttk.Treeview(root, columns=("Column1", "Column2"))
eframe = ttk.Frame(root)
eframe.pack(side="bottom", fill="x")
self.tree.pack(side="top", fill="both", expand=True)
self.e1 = ttk.Entry(eframe)
self.e2 = ttk.Entry(eframe)
self.e3 = ttk.Entry(eframe)
self.e1.pack(side="left", fill="x", expand=True)
self.e2.pack(side="left", fill="x", expand=True)
self.e3.pack(side="left", fill="x", expand=True)
for i in range(10):
self.tree.insert("", "end", text=f"Item #{i}", values=(f"{i}: a", f"{i}: b"))
self.tree.bind("<<TreeviewSelect>>", self.handle_selection)
def handle_selection(self, event):
self.e1.delete(0, "end")
self.e2.delete(0, "end")
self.e3.delete(0, "end")
selection = self.tree.selection()
if selection:
item = self.tree.item(selection)
self.e1.insert(0, item['text'])
self.e2.insert(0, item['values'][0])
self.e3.insert(0, item['values'][1])
Example()
tk.mainloop()