views:

127

answers:

3

I have a TreeCtrl in which more than one Item can be assigned the same object as PyData. When the object is updated, I want to update all of the items in the tree which have that object as their PyData.

I thought the following code would solve the problem quite neatly, but for some reason the logical test (current != self.GetFirstVisibleItem()) always returns true leading to infinite recursion. Can anyone explain why?

def RefreshNodes(self, obj, current=None):
    print "Entered refresh"
    current = current or self.GetFirstVisibleItem()
    if current.IsOk():
        print self.GetPyData(current).name
        if self.GetPyData(current) == obj:
            self.RefreshNode(current)
        current = self.GetNextVisible(current)
        if current != self.GetFirstVisibleItem():
            self.RefreshNodes(obj, current)

Edit: the above is obviously part of a class based on wx.TreeCtrl

A: 

There is no way for current != self.GetFirstVisibleItem() to be false. See comments below

def RefreshNodes(self, obj, current=None):
    print "Entered refresh"
    current = current or self.GetFirstVisibleItem()
    if current.IsOk():
        print self.GetPyData(current).name
        if self.GetPyData(current) == obj:
            self.RefreshNode(current)

        #current = next visible item
        current = self.GetNextVisible(current)

        #current can't equal the first visible item because
        # it was just set to the next visible item, which 
        # logically cannot be first
        if current != self.GetFirstVisibleItem():   
            self.RefreshNodes(obj, current)
tgray
+3  A: 

How is the "next" item ever going to be the first item?

This appears to be a tautology. The next is never the first.

    current = self.GetNextVisible(current)

    current != self.GetFirstVisibleItem()

It doesn't appear that next wraps around to the beginning. It appears that next should return an invalid item (IsOk is False) at the end.

See http://wxpython.org/onlinedocs.php for information on this.

S.Lott
+1: for linking to docs, and for using a word I had to look up ;)
tgray
GetNextVisibleItem does wrap, otherwise the function would end of it's own accord after going through the 6 items in my test tree (note that I'm already checking current.IsOk() earlier in the function)
mavnn
Oops. Found the problem, see my answer for details.
mavnn
A: 

Just realised the problem: if current is not a valid item, it's logical value is False.

Hence the line current = current or self.GetFirstVisibleItem() wraps back to the first item before current.IsOk() is called...

mavnn