Python: Tkinter Treeview Searchable

后端 未结 3 1876
一个人的身影
一个人的身影 2021-02-06 16:21

Fairly straight forward question, and despite my best Google-Fu I can\'t find anything on this.

I have a Python app that uses a Tkinter Treeview widget as a table. This

3条回答
  •  心在旅途
    2021-02-06 17:07

    You can define your own recursive method to search in the Treeview widget, and call selection_set on the appropiate child if its text starts with the content of the entry:

    import Tkinter as tk
    import ttk
    
    class App(tk.Frame):
        def __init__(self, master):
            tk.Frame.__init__(self, master)
            self.entry = tk.Entry(self)
            self.button = tk.Button(self, text='Search', command=self.search)
            self.tree = ttk.Treeview(self)
            # ...
        def search(self, item=''):
            children = self.tree.get_children(item)
            for child in children:
                text = self.tree.item(child, 'text')
                if text.startswith(self.entry.get()):
                    self.tree.selection_set(child)
                    return True
                else:
                    res = self.search(child)
                    if res:
                        return True
    

提交回复
热议问题