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

发布于 2024-11-27 23:31:20 字数 153 浏览 0 评论 0原文

我正在使用 wxPython 并得到了一棵包含一些项目的树。 现在我需要一个函数,它可以按名称提供树项对象。

例如: item = self.GetItemByName("MyStories")

我在文档中找不到这样的函数。

有人有什么想法吗?

I am using wxPython and got a tree with some items.
Now I need a function which give me the tree item object by name.

For example:
item = self.GetItemByName("MyStories")

I can not find such function in the documentation.

Does anyone has any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唱一曲作罢 2024-12-04 23:31:20

以下是查找具有特定标签的第一个树项目的一种方法:

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!')

但根据您在树中显示的内容,可能有一种更简单的方法来处理它。 TreeCtrl 已经提供了在填充树时在树项和其他对象之间创建引用的工具,并且字典查找比我刚刚键入的内容更快、更清晰。

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.

野生奥特曼 2024-12-04 23:31:20

虽然 robots.jpg 答案可行,但我发现更好的解决方案是跟踪字典中的 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

然后当您需要查找该项目时一个你可以获取tree_id的对象

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

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)

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 = self.tree_item_ids[obj.name]
data = self.tree.GetPyData(tree_id)
南城旧梦 2024-12-04 23:31:20

您还可以覆盖 TreeCtrl 并将 tree_ctrl_instance 更改为 self

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

You may also override TreeCtrl and change tree_ctrl_instance with self

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文