【问题标题】:Python Tkinter Treeview search all entriesPython Tkinter Treeview 搜索所有条目
【发布时间】:2018-04-28 14:42:52
【问题描述】:

我使用树视图来显示文件夹包含的所有项目。

我用这个函数创建所有条目:

def SUBS(path, parent):    
    for p in os.listdir(path):
        abspath = os.path.join(path, p)
        parent_element = tree.insert(parent, 'end', text=p, open=True)
        if os.path.isdir(abspath):
            SUBS(abspath, parent_element)

现在我想用这个函数搜索它:

def search(event, item=''): 
    children = tree.get_children(item)
    for child in children:
        text = tree.item(child, 'text')
        if text.startswith(entry.get()):
            tree.selection_set(child)

问题是搜索功能只搜索第一个“节点”。不是全部。那么我如何通过孩子的孩子进行搜索?又叫什么名字?

【问题讨论】:

    标签: python python-3.x search tkinter treeview


    【解决方案1】:

    为什么不在 SUBS 函数中使用递归?

    def search(event, item=''): 
        children = tree.get_children(item)
        for child in children:
            text = tree.item(child, 'text')
            if text.startswith(entry.get()):
                tree.selection_set(child)
            search(None, item=child) # Use the function for all the children, their children, and so on..
    

    【讨论】:

    • 我之前尝试过,但我没有将它用于孩子的孩子。对不起,如果这是终极初学者,但你能告诉我怎么做吗?
    • 我明白了!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 2023-04-04
    相关资源
    最近更新 更多