Python: Tkinter Treeview Searchable
Question: 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 works
fine for what I need to use it for, but there are going to end up being a
couple hundred items in a few of the trees.

Is there anyway to make a Treeview searchable, insofar as when the Tree has
focus, the user can just type a few characters and have the first alphabetical
match highlighted (rather than a separate entity in the window to type the
search pattern into)?


Answer: You can define your own recursive method to search in the Treeview widget, and
call
[`selection_set`](http://docs.python.org/2.7/library/ttk.html?highlight=treeview#ttk.Treeview.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
    

