简体   繁体   English

wxPython:TreeCtrl:如何按名称获取树项?

[英]wxPython: TreeCtrl: How can I get a tree item by name?

I am using wxPython and got a tree with some items. 我正在使用wxPython,并得到了带有某些项目的树。 Now I need a function which give me the tree item object by name. 现在,我需要一个函数,该函数按名称为我提供树项对象。

For example: item = self.GetItemByName("MyStories") 例如:item = self.GetItemByName(“ MyStories”)

I can not find such function in the documentation. 我在文档中找不到此类功能。

Does anyone has any ideas? 有人有什么想法吗?

Here's one way to find the first tree item with a specific label: 这是查找带有特定标签的第一个树项的一种方法:

def get_item_by_label(self, tree, search_text, root_item):
    item, cookie = tree.GetFirstChild(root_item)

    while item.IsOk():
        text = tree.GetItemText(item)
        if text.lower() == search_text.lower():
            return item
        if tree.ItemHasChildren(item):
            match = self.get_item_by_label(tree, search_text, item)
            if match.IsOk():
                return match
        item, cookie = tree.GetNextChild(root_item, cookie)

    return wx.TreeItemId()

result = get_item_by_label(tree, 'MyStories', tree.GetRootItem())
if result.IsOk():
    print('We have a match!')

But depending on what you're displaying in the tree, there's probably an easier way to handle it. 但是根据您在树中显示的内容,可能有一种更简单的处理方法。 The TreeCtrl already provides the tools to create references both ways between tree items and other objects as you fill the tree, and dict lookups are much faster and cleaner looking than what I just typed. TreeCtrl已经提供了用于在填充树时在树项目和其他对象之间创建引用的工具,并且dict查找比我刚才键入的查找更快,更干净。

While robots.jpg answer will work but I find a much better solution is to track the ids in a dict like the following (hinted at by @robots.jpg & @Steven Sproat) 虽然robots.jpg答案有效,但我发现更好的解决方案是在如下所示的dict中跟踪ID(由@ robots.jpg和@Steven Sproat提示)

self.tree_item_ids = {}
root = self.tree.GetRootItem()
for obj in objs_to_add:
    tree_id = self.tree.AppendItem(root,obj.name)
    self.tree_item_ids[obj.name] = tree_id

and then later when you need to lookup the item for an object you can just grab the tree_id 然后,当您需要查找对象的项目时,只需抓住tree_id

tree_id = self.tree_item_ids[obj.name]
data = self.tree.GetPyData(tree_id)

You may also override TreeCtrl and change tree_ctrl_instance with self 您也可以覆盖TreeCtrl并使用self更改tree_ctrl_instance

def GetItemByName(self, search_text, tree_ctrl_instance):
        retval = None
        root_list = [tree_ctrl_instance.GetRootItem()]
        for root_child in root_list:
            item, cookie = tree_ctrl_instance.GetFirstChild(root_child)
            while item.IsOk():
                if tree_ctrl_instance.GetItemText(item) == search_text:
                    retval = item
                    break
                if tree_ctrl_instance.ItemHasChildren(item):
                    root_list.append(item)
                item, cookie = tree_ctrl_instance.GetNextChild(root_child, cookie)
        return retval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM