我已经设置了一个树视图来显示我的sqlite数据库中的数据。我能够用查询填充第一列,现在我需要用不同的查询填充其他列。
# Top Container
top_frame = ttk.Frame()
top_frame.grid(column=0, row=0)
# create a treeview with dual scrollbars
list_header = ['First', 'Second', 'Third']
self.tree = ttk.Treeview(columns=list_header, show="headings")
vsb = ttk.Scrollbar(orient="vertical",
command=self.tree.yview)
hsb = ttk.Scrollbar(orient="horizontal",
command=self.tree.xview)
self.tree.configure(yscrollcommand=vsb.set,
xscrollcommand=hsb.set)
self.tree.grid(column=0, row=0, sticky=(N, S, E, W), in_=top_frame)
vsb.grid(column=1, row=0, sticky=(N, S), in_=top_frame)
hsb.grid(column=0, row=1, sticky=(E, W), in_=top_frame)
# Display the headers
for col in list_header:
self.tree.heading(col, text=col.title(), command=lambda c=col: sortby(self.tree, c, 0))
# adjust the column's width to the header string
self.tree.column(col)
# Display the data
query = ProjectNumber.select()
for item in query:
self.tree.insert('', 'end', values=item.project_num)
how can i access the next column and fill it with a separate query?
如果我明白了,下面你可以看到你想做的一个有效的例子。
这个例子是用oo方法写的。
启动脚本后单击“加载”按钮,您将看到填充的树视图
模拟记录集的数据,元组列表。尝试点击后的项目
树视图,看看会发生什么。我添加了偶数点击和双击事件回调。
数据代表意大利面食。
import tkinter as tk
from tkinter import ttk
class App(tk.Frame):
def __init__(self,):
super().__init__()
self.master.title("Hello World")
self.init_ui()
def init_ui(self):
f = tk.Frame()
tk.Label(f, text = "Buon Appetito").pack()
cols = (["#0",'','w',False,200,200],
["#1",'','w',True,0,0],)
self.Pasta = self.get_tree(f, cols, show="tree")
self.Pasta.show="tree"
self.Pasta.pack(fill=tk.BOTH, padx=2, pady=2)
self.Pasta.bind("<<TreeviewSelect>>", self.on_selected)
self.Pasta.bind("<Double-1>", self.on_double_click)
w = tk.Frame()
tk.Button(w, text="Load", command=self.set_values).pack()
tk.Button(w, text="Close", command=self.on_close).pack()
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
def set_values(self,):
#.insert(parent, index, iid=None, **kw)
rs = [(0,'Spaghetti'),(1,'Rigatoni'),(2,'Pennette')]
for i in rs:
#print(i)
pasta = self.Pasta.insert("", i[0], text=i[1], values=(i[0],'pasta'))
rs_dishes = self.load_dishes(i[0])
if rs_dishes is not None:
for dish in rs_dishes:
#print(dish)
wards = self.Pasta.insert(pasta, dish[0],text=dish[1], values=(dish[0],'dishes'))
def load_dishes(self, i):
rs = [(0,'Spaghetti aglio e olio'),
(1,'Rigatoni alla matriciana'),
(1,'Rigatoni al pesto'),
(1,'Rigatoni alla norma'),
(2,'Pennette al pesto'),
(2,'Pennette alla wodka'),
(2,'Pennette al tartufo'),
(0,'Spaghetti allo scoglio'),
(0,'Spaghetti al pesto'),
(0,'Spaghetti alla carbonara'),
(0,'Spaghetti alla puttanesca')]
r = [x for x in rs if x[0] == i]
return r
def on_selected(self, evt=None):
selected_item = self.Pasta.focus()
d = self.Pasta.item(selected_item)
if d['values']:
if d['values'][1]=='dishes':
pk = d['values'][0]
print("pk: {}".format(pk))
def on_double_click(self, evt=None):
if self.Pasta.focus():
item_iid = self.Pasta.selection()
pk = self.Pasta.item(self.Pasta.focus())['text']
print(item_iid, pk)
def get_tree(self,container, cols, size=None, show=None):
headers = []
for col in cols:
headers.append(col[1])
del headers[0]
if show is not None:
w = ttk.Treeview(container,show=show)
else:
w = ttk.Treeview(container,)
w['columns']=headers
for col in cols:
w.heading(col[0], text=col[1], anchor=col[2],)
w.column(col[0], anchor=col[2], stretch=col[3],minwidth=col[4], width=col[5])
sb = ttk.Scrollbar(container)
sb.configure(command=w.yview)
w.configure(yscrollcommand=sb.set)
w.pack(side=tk.LEFT, fill=tk.BOTH, expand =1)
sb.pack(fill=tk.Y, expand=1)
return w
def on_close(self):
self.master.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()