[英]Python Tkinter Treeview search all entries
我用樹形視圖來顯示文件夾中包含的所有項目。
我創建這個函數的所有條目:
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..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.