[wxPython] directed traversal of a wxTreeCtrl

I'm looking for the accepted wxPythonic idiom for doing what I think must be a common task.

I have a wxTreeCtrl in which is represented a data model. Some expandable items represent objects. Each of those objects is the root of a sub-tree which reflects its internal structure. Two objects of the same class will be represented in separate sub-trees having identical structure.

An operation on an object will affect some sub-item of its representation in the wxTreeCtrl. I have the item and I know what class object it represents, so I know the traversal that will get me from the object root to the sub-item I want to manipulate. How do wxPythonistas normally do this?

By way of example, let's say that somewhere in my wxTreeCtrl is an item that represents a Whozits. A Whozits has lots of interesting bits and pieces, one of which is a Thingamabob, whose tree item must turn purple under certain circumstances. The Thingamabob for a Whosits is always the second child of the third child of the first child of the toplevel Whozits item:

+- uninteresting stuff

+- The Whozits I'm working with

    >
    +- A collection of Whozits parts
    > >
    > +- Some Part
    > >
    > +- Another Part
    > >
    > +- Yet Another Part
    > >
    > +- Almost What I want
    > >
    > +- The Thingamabob I want
    >
    +- Other Stuff in a Whozits

+- A different Whozits I don't care about right now

+- more uninteresting stuff

Right now, I'm doing the following to get from a Whozits to a Thingamabob:

  cookie = 0
  c2, cookie = tree.GetFirstChild(whozits, cookie)

  c3, cookie = tree.GetFirstChild(c2, cookie)
  c3, cookie = tree.GetNextChild(c2, cookie)

  c4, cookie = tree.GetFirstChild(c3, cookie)
  c4, cookie = tree.GetNextChild(c3, cookie)

  print 'thingamabob is item', c4

It works, but it's ugly and it certainly doesn't scale well. I'm about to generalize this in a helper routine that takes a tree, a starting point, and a list representing the traversal path:

     thingamabob = traverseTree(tree, from = whozits, route = [1, 3, 2])

If there's a standard way to do this, I'd be happy if someone would point me in the right direction.

Regards,

Greg Goodman

Well, I won't claim that I know the accepted wxPythonic idiom, and
I am not even sure that what I am doing is quite what you want to
do, but for what it is worth, here is how I create and traverse a
wxTreeCtrl for a data model.

ยทยทยท

--- Greg Goodman <chironsw@swbell.net> wrote:

I have a wxTreeCtrl in which is represented a data model. Some
expandable items represent objects. Each of those objects is the
root of a sub-tree which reflects its internal structure. Two
objects of the same class will be represented in separate sub-
trees having identical structure.

An operation on an object will affect some sub-item of its
representation in the wxTreeCtrl. I have the item and I know
what class object it represents, so I know the traversal that
will get me from the object root to the sub-item I want to
manipulate. How do wxPythonistas normally do this?

=======
class Simple(object):

    def Branch(self, tree):
        """Add this element as a leaf to the wxTreeCtrl."""
        name = self.Get()
        if name == '':
            name = '.'
        leaf = tree.AppendItem(tree.trunk[-1], name)
        tree.SetPyData(leaf, self)

class ComplexElement(object):

    def TreeCtrl(self, *args, **kwargs):
        # self is the element
        return self.treeCtrl(self, *args, **kwargs)

    class treeCtrl(wxTreeCtrl):

        def __init__(self, element, *args, **kwargs):
            wxTreeCtrl.__init__(self, *args, **kwargs)
            self.trunk = [self.AddRoot("Root")]
            # self is the tree
            element.Branch(self)

    def Branch(self, tree):
        for name in dir(self):
            attr = getattr(self, name)
            # leave this conditional out to include ALL attr
            if isinstance(attr, Storable):
                leaf = tree.AppendItem(tree.trunk[-1], name)
                tree.SetPyData(leaf, self)
                tree.trunk.append(leaf)
                attr.Branch(tree) # recursive!
                tree.trunk = tree.trunk[:-1]

class ElementList(object):
    
    def Branch(self, tree):
        for item in self:
            name = item.__class__.__name__
            leaf = tree.AppendItem(tree.trunk[-1], name)
            tree.trunk.append(leaf)
            item.Branch(tree) # recursive!
            tree.trunk = tree.trunk[:-1]

=====
Donnal Walter
Arkansas Children's Hospital

__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax