【问题标题】:search and select item in treeview tkinter python在treeview tkinter python中搜索和选择项目
【发布时间】:2021-04-19 08:52:17
【问题描述】:

我想创建search_function 来搜索树视图表中的数据。 在我的代码中,我确实需要在下面的 3 个请求之后单击 search_button 以在树视图表中选择数据。

from tkinter import *
from tkinter import ttk

GUI = Tk()
GUI.geometry('1920x1080')

tree_view_frame = Frame(GUI)
tree_view_frame.pack()
tree_scroll = Scrollbar(tree_view_frame)
tree_scroll.pack(side = RIGHT, fill = Y)
header = ['Part No.', 'Name', 'Unit', 'Quan', 'Price','Total', 'Initial Total']
hdsize = [60,240,60,60,70,80,80]
aanchor = [W,W,E,E,E,E,E]
global product_table
product_table = ttk.Treeview(tree_view_frame, columns = header, show = 'headings', height = 20, yscrollcommand=tree_scroll.set, selectmode="extended")
def treeview_sort_column(product_table, col, reverse):
    l = [(product_table.set(k, col), k) for k in product_table.get_children('')]
    l.sort(reverse=reverse)
    for index, (val, k) in enumerate(l):
        product_table.move(k, '', index)
    product_table.heading(col, command=lambda _col=col: treeview_sort_column(product_table, _col, not reverse))
for col in header:
    product_table.heading(col, text=col,command=lambda _col=col: treeview_sort_column(product_table, _col, False))
product_table.pack()
tree_scroll.config(command = product_table.yview)
for h,s,a in zip(header, hdsize, aanchor):
    product_table.heading(h, text = h)
    product_table.column(h,width = s, anchor = a)

Nameget = StringVar()
E_namme = ttk.Entry(GUI,textvariable = Nameget)
E_namme.pack()

def Add_Product(event = None):
    product_table.insert('', 'end', value = ('', Nameget.get()))
B_Add_product = ttk.Button(GUI, text = "Add Product", command = Add_Product)
B_Add_product.pack()

def search_function():
    query = Nameget.get()
    selections = []
    for child in product_table.get_children():
        if query in product_table.item(child)['values'][1]:
            selections.append(child)
            product_table.selection_set(selections[0])
            product_table.focus(selections[0])
    product_table.focus_force()
search_button = ttk.Button(GUI, text = "search_button", width = 15, command = search_function)
search_button.pack()

GUI.mainloop()
  1. 第一次点击是选择第一个结果。

  2. 第二次或下一次单击是选择下一个结果直到最后一个结果并将其作为循环。

  3. 当结果未出现在当前页面时,自动向下滚动滚动条以选择结果。

在当前代码中,它只能选择第一个结果,不能自动向下滚动滚动条。

提前谢谢你。

【问题讨论】:

    标签: python tkinter treeview treeviewitem


    【解决方案1】:

    您可以使用see(确保该项目可见)并循环。

    任何数据结构都可以在这里使用queue 或dict,如下例所示:

    (使用一些OOP很好)

    from tkinter import *
    from tkinter import ttk
    from collections import defaultdict
    
    GUI = Tk()
    GUI.geometry("1920x1080")
    
    tree_view_frame = Frame(GUI)
    tree_view_frame.pack()
    tree_scroll = Scrollbar(tree_view_frame)
    tree_scroll.pack(side=RIGHT, fill=Y)
    header = ["Part No.", "Name", "Unit", "Quan", "Price", "Total", "Initial Total"]
    hdsize = [60, 240, 60, 60, 70, 80, 80]
    aanchor = [W, W, E, E, E, E, E]
    global product_table
    
    
    class ProductMap:
        def __init__(self):
            self.selections = defaultdict(list)
            self.last_lookup = ""
    
        def treeview_sort_column(self, product_table, col, reverse):
            l = [(product_table.set(k, col), k) for k in product_table.get_children("")]
            l.sort(reverse=reverse)
            for index, (val, k) in enumerate(l):
                product_table.move(k, "", index)
            product_table.heading(
                col,
                command=lambda _col=col: product_map.treeview_sort_column(
                    product_table, _col, not reverse
                ),
            )
    
        def search_function(self):
            query = Nameget.get()
            if not query:
                return
            children = product_table.get_children()
            for child in children:
                curr = product_table.item(child)["values"][1]
                if query in curr and child not in self.selections[query]:
                    self.selections[query].append(child)
                    product_table.selection_set(child)
                    product_table.focus(child)
                    product_table.see(child)
                    self.last_lookup = query
                    return
                elif query != self.last_lookup:
                    self.selections = defaultdict(list)
    
        def Add_Product(self, event=None):
            product_table.insert("", "end", value=("", Nameget.get()))
    
    
    product_map = ProductMap()
    
    product_table = ttk.Treeview(
        tree_view_frame,
        columns=header,
        show="headings",
        height=20,
        yscrollcommand=tree_scroll.set,
        selectmode="extended",
    )
    for col in header:
        product_table.heading(
            col,
            text=col,
            command=lambda _col=col: product_map.treeview_sort_column(
                product_table, _col, False
            ),
        )
    product_table.pack()
    tree_scroll.config(command=product_table.yview)
    for h, s, a in zip(header, hdsize, aanchor):
        product_table.heading(h, text=h)
        product_table.column(h, width=s, anchor=a)
    
    Nameget = StringVar()
    E_namme = ttk.Entry(GUI, textvariable=Nameget)
    E_namme.pack()
    
    
    B_Add_product = ttk.Button(GUI, text="Add Product", command=product_map.Add_Product)
    B_Add_product.pack()
    
    
    search_button = ttk.Button(
        GUI, text="search_button", width=15, command=product_map.search_function
    )
    search_button.pack()
    
    
    GUI.mainloop()
    

    【讨论】:

    • 谢谢,我的第三个请求有效,但第二个请求未修复。
    • 代码将所选结果从第一行移动到其他行。但我需要的只是将选择线从一个结果移动到另一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2020-09-24
    • 2020-10-22
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多