[wxPython] How to tack data onto a node of a tree?

Steve Lamb grey at despair.rpglink.com
Sat Aug 9 20:03:52 CEST 2003

Hash: SHA1

    Ok, this one has me scratching my head.  Right now my application is
working fine.  I have a tree control which contains a 2-level deep tree.
The first level is directories while the second level are files in those
directories.  The user clicks on the file they want to manipulate and it
is loaded and parsed into other windows.  However I'm doing it in a
kludgey fashion and would like to do the proper thing.

    The kludge is that I could not figure out how to associate data with
a particular node within the tree.  So for the moment I have set the
data to NULL and the node's name is either the directory name or the
file name.  When the user clicks on the file I then grab the node's
name, grab the parent node ID, grab the name of the parent node name and
then splice them together.  EG:

    def BuildPath(self, id):
        ''' Build a path from the node and parent node name'''
        # This really should be contained in the node data.
        file = self.FileList.GetItemText(id)
        parent = self.FileList.GetItemParent(id)
        path = self.FileList.GetItemText(parent)
        filepath = path + '/' + file

    As the comment says, the path to the file should really be contained
in the data associated with the node.  

    I've read the documentation for wxTreeItemData and am a little
confused.  Would something like the following work for assignment?

dir = '/dir/to/file'
file = 'filename'
filepath = wxTreeItemData(dir + '/' + file)
self.FileList.Append(dir_node, -1, -1, filepath)

    I believe that the following would be used to retrieve the data?
filepath = self.FileList.GetPyData(id)

Version: GnuPG v1.2.2 (GNU/Linux)


	                       |    -- Lenny Nero - Strange Days

More information about the Python-list mailing list