Search code examples
pythonwxpythonwxtextctrl

wx.TreeCtrl item


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


Solution

  • 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)