wx.TreeCtrl 项目

发布于 2024-11-07 00:01:54 字数 793 浏览 4 评论 0原文

我正在尝试使用 TreeCtrl 来表示文件夹结构。对于每个文件夹,我需要知道它的绝对路径和名称。我目前正在做这样的事情:

self.root = self.tree.AddRoot(project.name)
self.tree.SetPyData(self.root, None)
self.root.path = root 

---- other code -----

childItem = self.tree.AppendItem(self.root, child.name)
childItem.path = self.root.path + "/" +  child.name

但现在在一个事件中我需要获取路径字符串。到目前为止,我失败的方法是:

self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnItemExpanded, self.tree)

----- other code -------

def OnItemExpanded(self, evt):
    selected = evt.GetItem()
    print selected.path

现在失败了,因为: AttributeError: 'TreeItemId' object has no attribute 'path' 。根据我在这里的理解,该事件只为我提供了树中某个项目的 Id,而不是由“childItem = self.tree.AppendItem(self.root, child.name)”产生的实际项目?如果是这种情况,我怎样才能找到该项目?

问候, 博格丹

I'm trying to use a TreeCtrl to represent a folder structure. For each folder I need to know it's absolute path and name. I'm currently doing something like this:

self.root = self.tree.AddRoot(project.name)
self.tree.SetPyData(self.root, None)
self.root.path = root 

---- other code -----

childItem = self.tree.AppendItem(self.root, child.name)
childItem.path = self.root.path + "/" +  child.name

But now on an event I will need to get the path string. So far my approach that fails is:

self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnItemExpanded, self.tree)

----- other code -------

def OnItemExpanded(self, evt):
    selected = evt.GetItem()
    print selected.path

Now this fails because: AttributeError: 'TreeItemId' object has no attribute 'path' . From what I understand here the event only gives me a Id to a Item from the tree and not the actual Item that resulted from the "childItem = self.tree.AppendItem(self.root, child.name)" ? If that is the case how can I get to that item ?

regards,
Bogdan

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

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

发布评论

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

评论(1

橘虞初梦 2024-11-14 00:01:54

.path 属性是什么?这是您正在创建的东西还是 TreeItemId 对象的实际成员(这是从“AppendItem”方法返回的对象)?我没有看到任何关于它的文档。

如果要在子项中存储任意数据,请使用 SetPyData/GetPyData 方法。

childItem = self.tree.AppendItem(self.root, child.name)
self.tree.SetPyData(childItem, ["hi", "i" , "am", "a", "python", "object"])

然后在你的处理程序中:

def OnItemExpanded(self, event):
    item = event.GetItem()
    if item:
        pyObj = self.tree.GetPyData(item)

What is the .path property? Is this something you are creating or an actual member of the TreeItemId object (this is the object returned from the "AppendItem" method)? I do not see any docs on it.

If you want to store arbitrary data in the child items use SetPyData/GetPyData methods.

childItem = self.tree.AppendItem(self.root, child.name)
self.tree.SetPyData(childItem, ["hi", "i" , "am", "a", "python", "object"])

Then in your handler:

def OnItemExpanded(self, event):
    item = event.GetItem()
    if item:
        pyObj = self.tree.GetPyData(item)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文