Recursively expand all branches ox wxTreeCtrl

P

Piet

Hi there.
I have a problem when working with a wxTreeCtrl. I would like to
expand all branches of a sepcific position in a tree with a single
command. Since there does not appear to be such a command, I tried to
write my own recursive function. Here is the code snippet:
def OnPopup1(self,event):
item = self.Tree.GetSelection()
self.parent.msgbox(self,self.Tree.GetItemText(item),"Kein
Titel",wxOK)
#self.Tree.Expand(item)
self.ExpandCompleteBranch(self.Tree,item,0)

def ExpandCompleteBranch(self,tree,treeitem,cookie):
self.parent.msgbox(self,"Durchlauf "+str(cookie),"Kein
Titel",wxOK)
if tree.ItemHasChildren(treeitem):
lastchild = tree.GetLastChild(treeitem)
tree.Expand(treeitem)
(child,cookie) = tree.GetFirstChild(treeitem,cookie)
self.ExpandCompleteBranch(tree,child,cookie+1)
while child != lastchild:
cookie = cookie +1
(child,cookie) = tree.GetNextChild(treeitem,cookie)
self.ExpandCompleteBranch(tree,child,cookie+1)
Don´t worry aout the self.parent.msgbox-function. This just calls the
wxMessageDialog, gets the GetModal and closes the dialog. The
msgbox-function works fine but the recursive complete branch expanding
does not. When ativating the function, I first get the value "0" in
the MEssageDialog, then the selected node is expanded, then I get a
very high number (> 4 million), then the first child of the originally
selected node is expanded, I get a very high value again in the
cookie-MessageDialog, and then I get only MessageDialogs with the
value "1", and nothing happens to the tree.
Can anybody help. I think that the error lies in the assignment of the
values for the cookies, but I don´t know how these things work.
Can anybody help?
 
J

Josiah Carlson

Try the following modifications...

def OnPopup1(self,event):
item = self.Tree.GetSelection()
self.parent.msgbox(self,self.Tree.GetItemText(item),"Kein
Titel",wxOK)
#self.Tree.Expand(item)
* self.ExpandCompleteBranch(self.Tree,item,wxNewId())

def ExpandCompleteBranch(self,tree,treeitem,cookie):
self.parent.msgbox(self,"Durchlauf "+str(cookie),"Kein Titel",wxOK)
if tree.ItemHasChildren(treeitem):
lastchild = tree.GetLastChild(treeitem)
tree.Expand(treeitem)
(child,cookie) = tree.GetFirstChild(treeitem,cookie)
self.ExpandCompleteBranch(tree,child,cookie+1)
while child != lastchild:
* #cookie = cookie + 1
(child,cookie) = tree.GetNextChild(treeitem,cookie)
* self.ExpandCompleteBranch(tree,child,wxNewId())

I use wxNewId() for cookie ids so that all calls will have unique cookie
ids. I'm not sure this is technically necessary, but it can't hurt.

Also, by altering the cookie id during the while loop, you are
destroying the cookie that is necessary to access the remainder of the
branch.

Give the above (without the '*' at the beginning of the line) a try.

- Josiah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top