【问题标题】:How can I directly access the next column in my treeview to insert data如何直接访问树视图中的下一列以插入数据
【发布时间】:2019-08-24 00:49:01
【问题描述】:

我已经设置了一个树视图来显示我的 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?

【问题讨论】:

    标签: python-3.x sqlite tkinter treeview peewee


    【解决方案1】:

    如果我理解了,您可以在下面看到您想要做的工作示例。

    这个例子是用oo方法写的。

    启动脚本后点击加载按钮,你会看到树视图填充了

    模拟记录集的数据,元组列表。尝试点击一个项目后

    treeview 看看会发生什么。我添加了偶数单击和双击事件回调。

    数据代表意大利面食。

    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()
    

    【讨论】:

    • 我找到了如何通过将 self.tree.insert('', 'end', values=item.project_num) 更改为 self.tree.insert('', 'end' 来访问下一列, values=(item.project_num, item.project_name)),类似于你所拥有的人。我喜欢你添加的回调,我接下来要研究它。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多