Python:Tkinter Treeview 可搜索 [英] Python: Tkinter Treeview Searchable

查看:185
本文介绍了Python:Tkinter Treeview 可搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当直接的问题,尽管我使用了最好的 Google-Fu,但我找不到任何关于此的信息.

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

我有一个 Python 应用程序,它使用 Tkinter Treeview 小部件作为表格.这对于我需要使用它的情况很有效,但最终会在一些树中产生几百个项目.

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.

无论如何要使 Treeview 可搜索,只要当 Tree 具有焦点时,用户只需键入几个字符并突出显示第一个字母匹配项(而不是窗口中的单独实体将搜索模式输入到)?

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

推荐答案

您正在寻找的功能不是现成的,但您可以轻松地对其进行编码.

The feature you are looking for does not exists out of the box, but you can easily code it.

简而言之:子类Treeview小部件,当在小部件上按下一个键时,在右上角显示一个条目(允许您叠加小部件),完成后,删除该条目.

In short: subclass Treeview widget, when a key is pressed on your widget, display an entry in the top right corner (place allow you to superimpose widgets), when you are done, remove the entry.

这里有一些片段:

实例化您的附加小部件和绑定.

Instantiate your additionals widgets and your bindings.

class SearchableTreeview(ttk.Treeview):
    def __init__(self, *args, **kwargs):
        ttk.Treeview.__init__(self, *args, **kwargs)
        #create the entry on init but does no show it
        self._toSearch = tk.StringVar()
        self.entry = tk.Entry(self, textvariable=self._toSearch)

        self.bind("<KeyPress>", self._keyOnTree)
        self._toSearch.trace_variable("w", self._search)
        self.entry.bind("<Return>", self._hideEntry)
        self.entry.bind("<Escape>", self._hideEntry)

2) 临时入境

当按下一个键时,显示带有位置的条目.entry.place(relx=1, anchor=tk.NE) 将在右上角的树上方显示条目.如果按下的键是字母,请在条目中复制该字母.将焦点设置在条目上,以便随后的按键落入其中.

2) temporary entry

When a key is hit, show the entry with place. entry.place(relx=1, anchor=tk.NE) will show the entry above the tree in the top right corner. If the key pressed is a letter, copy this letter in the entry. Set the focus on the entry so that following key press land in it.

对称地,当 Escape 或 Return 在条目上被击中时,刷新内容,隐藏条目(place_forget),并将焦点设置到树上(否则,条目保持焦点,即使不可见).

Symmetrically, when Escape or Return is hit while on the entry, flush the content, hide the entry (place_forget), and set the focus to the tree (otherwise, the entry keep the focus, even if not visible).

    def _keyOnTree(self, event):
        self.entry.place(relx=1, anchor=tk.NE)
        if event.char.isalpha():
            self.entry.insert(tk.END, event.char)
        self.entry.focus_set()

    def _hideEntry(self, event):
        self.entry.delete(0, tk.END)
        self.entry.place_forget()
        self.focus_set()

3) 实际搜索代码

您可以随意搜索您想要的项目(深度或广度优先,匹配开始或完整字符串...).这是一个重复使用罗达斯的答案并忽略大小写的示例.

    def _search(self, *args):
        pattern = self._toSearch.get()
        #avoid search on empty string
        if len(pattern) > 0:
            self.search(pattern)

    def search(self, pattern, item=''):
        children = self.get_children(item)
        for child in children:
            text = self.item(child, 'text')
            if text.lower().startswith(pattern.lower()):
                self.selection_set(child)
                self.see(child)
                return True
            else:
                res = self.search(pattern, child)
                if res:
                    return True

这篇关于Python:Tkinter Treeview 可搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆