python - Python Tkinter Treeview 搜索所有条目
问题描述
我使用树视图来显示文件夹包含的所有项目。
我使用此功能创建所有条目:
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)
问题是搜索功能只搜索第一个“节点”。不是全部。那么我如何通过孩子的孩子进行搜索?他们怎么称呼?
解决方案
为什么不在 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..
推荐阅读
- html - 单击 md-menu、mdDialog 按钮时,顶部导航向上移动
- django - 在 django 的 CreateView 中创建新的父模型
- python - 将 scipy.ndimage.convolve 应用于三维 xarray DataArray
- jquery - 如何使用列值在 jQuery DataTables 中组合两行或多行?
- c - C 中管道、greps、wc 的堆栈粉碎问题
- matlab - 查找任意数量的向量的第一次出现
- javascript - 动态添加不同数组中的对象
- rust - 为什么生命周期规则有利于字符串文字?
- laravel - 如何防止两个 Laravel 中间件创建无休止的重定向?
- javascript - 如何使用 React Native 访问对象的属性?