wxPython:TreeCtrl:如何按名称获取树项目?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是查找具有特定标签的第一个树项目的一种方法:
但根据您在树中显示的内容,可能有一种更简单的方法来处理它。 TreeCtrl 已经提供了在填充树时在树项和其他对象之间创建引用的工具,并且字典查找比我刚刚键入的内容更快、更清晰。
Here's one way to find the first tree item with a specific label:
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.
虽然 robots.jpg 答案可行,但我发现更好的解决方案是跟踪字典中的 id,如下所示(由 @robots.jpg 和 @Steven Sproat 暗示),
然后当您需要查找该项目时一个你可以获取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)
and then later when you need to lookup the item for an object you can just grab the tree_id
您还可以覆盖 TreeCtrl 并将 tree_ctrl_instance 更改为 self
You may also override TreeCtrl and change tree_ctrl_instance with self