looping through items in TreeCtrl?

hi, i can't figure out how to successfully loop through items under
another item in TreeCtrl. For example, i have the following tree:

root
-- item
----1
----2
----3
----4
----5

However, i don't usually know how many item i have under a certain
parent, how do i use GetNextChild() to loop through it? I found
GetCount(self), though that doesn't take in any parameters, so i'm
guessing it only gets how many items under root?

I was thinking of looping like
for itemNum in range(numberOfItem):
      child = tree.GetNextChild(item);
      #do something with the child

Though i cannot find any method that gets the number of items

I'm sure i'm doing something wrong here, can someone please point out
the errors? thanks alot!

···

--
"It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
Calvin in "Calvin and Hobbes"

If you call GetNextChild() when there is no next child, it's not an
error --- you just get back an invalid TreeItemId.

So you can do something like this, I think:

child, cookie = tree.GetFirstChild(root)
while child.IsOk():
    # do something with child
    child = tree.GetNextChild(root, cookie)

[where tree is your wx.TreeCtrl and root is the wx.TreeItemId whose
children you want to examine]

Although I haven't tested this :slight_smile:

···

On 10/02/06, Jason Wang <randomtalk@gmail.com> wrote:

However, i don't usually know how many item i have under a certain
parent, how do i use GetNextChild() to loop through it? I found
GetCount(self), though that doesn't take in any parameters, so i'm
guessing it only gets how many items under root?

--
John.

If you call GetNextChild() when there is no next child, it's not an
error --- you just get back an invalid TreeItemId.

So you can do something like this, I think:

child, cookie = tree.GetFirstChild(root)
while child.IsOk():
    # do something with child
    child = tree.GetNextChild(root, cookie)

[where tree is your wx.TreeCtrl and root is the wx.TreeItemId whose
children you want to examine]

Although I haven't tested this :slight_smile:

mmm.. i can't seem to find anything to sub for IsOk(), as there isn't
really any method to check if an item id is valid (at least i haven't
found it), can someone please point me in the right direction? thanks
alot!

···

--
"It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
Calvin in "Calvin and Hobbes"

one small error in the code John posted... it should have been:

            (child, cookie) = self.tree.GetFirstChild(root)

            while child.IsOk():
                (child, cookie) = self.tree.GetNextChild(root, cookie)

he misquoted the Bible (Demo) :wink:

···

On 2/10/06, Jason Wang <randomtalk@gmail.com> wrote:

> If you call GetNextChild() when there is no next child, it's not an
> error --- you just get back an invalid TreeItemId.
>
> So you can do something like this, I think:
>
> child, cookie = tree.GetFirstChild(root)
> while child.IsOk():
> # do something with child
> child = tree.GetNextChild(root, cookie)
>
> [where tree is your wx.TreeCtrl and root is the wx.TreeItemId whose
> children you want to examine]
>
> Although I haven't tested this :slight_smile:
>
mmm.. i can't seem to find anything to sub for IsOk(), as there isn't
really any method to check if an item id is valid (at least i haven't
found it), can someone please point me in the right direction? thanks
alot!
--
"It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
Calvin in "Calvin and Hobbes"

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

well, when i ran the code, it says Tuple object has no attribute
IsOk(), so i assume i suppose to write something instead of IsOk()...
Anyone know what's wrong?

···

On 2/10/06, Peter Damoc <pdamoc@gmail.com> wrote:

one small error in the code John posted... it should have been:

            (child, cookie) = self.tree.GetFirstChild(root)

            while child.IsOk():
                (child, cookie) = self.tree.GetNextChild(root, cookie)

he misquoted the Bible (Demo) :wink:

--
"It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
Calvin in "Calvin and Hobbes"

I'm sure you tried the code I posted and due to some voodoo magic the
child turned into a tuple. :slight_smile:
:slight_smile:
Peter

···

On 2/10/06, Jason Wang <randomtalk@gmail.com> wrote:

On 2/10/06, Peter Damoc <pdamoc@gmail.com> wrote:
> one small error in the code John posted... it should have been:
>
> (child, cookie) = self.tree.GetFirstChild(root)
>
> while child.IsOk():
> (child, cookie) = self.tree.GetNextChild(root, cookie)
>
> he misquoted the Bible (Demo) :wink:
well, when i ran the code, it says Tuple object has no attribute
IsOk(), so i assume i suppose to write something instead of IsOk()...
Anyone know what's wrong?
--
"It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
Calvin in "Calvin and Hobbes"

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

well, when i tried it on the TreeCtrl demo, it doesn't seem to be working..

I changed OnItemExpanded to:
def OnItemExpanded(self, event):
        item = event.GetItem()
        if item:
            (child, cookie) = self.tree.GetFirstChild(item)
            self.log.WriteText("first child: %s\n" %
self.tree.GetItemText(child))
            while child.IsOk():
                self.tree.Delete(child)
                (child, cookie) = self.tree.GetNextChild(root, cookie)
            self.log.WriteText("OnItemExpanded: %s\n" %
self.tree.GetItemText(item))

in the code, the first child printed fine if it's a child without
further child, however, it doesn't seem to work on child with further
divisions..

and it definitely doesn't delete any child..

···

On 2/10/06, Peter Damoc <pdamoc@gmail.com> wrote:

I'm sure you tried the code I posted and due to some voodoo magic the
child turned into a tuple. :slight_smile:
:slight_smile:
Peter

--
"It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
Calvin in "Calvin and Hobbes"