TreeListCtrl

23 views
Skip to first unread message

werner

unread,
Nov 27, 2009, 4:06:30 AM11/27/09
to wxPytho...@googlegroups.com
I can't figure out how to get the first child in a treelist.

Googled and checked the wiki and the demo but don't see a sample for this.

i.e. want to use this:

tree.GetFirstChild(0)

But whatever I fee to GetFirstChild it complains that it wants a
wxTreeItemId, but I can't figure out how to pass that to it (also the
doc for it says: "wxTreemItemIds are not meant to be constructed
explicitly by the user....".

Any hints would be very much appreciated.
Werner

Steven Sproat

unread,
Nov 27, 2009, 4:42:04 AM11/27/09
to wxpytho...@googlegroups.com
When you call AddRoot/AppendItem/InsertItem/InsertItemBefore, it returns
a TreeItemId. Keep a reference to these (a list is nice, I find) and
then pass whichever ItemId you want to GetFirstChild

Andrea Gavana

unread,
Nov 27, 2009, 4:50:20 AM11/27/09
to wxpytho...@googlegroups.com
Hi,

2009/11/27 Steven Sproat:
Don't do this unless you are using HyperTreeList.

"""
The wx.TreeItemId is a temporary opaque object that can *only* be used
for passing to/from treectl methods. It has no other utility, and due
to it's nature as a proxy to a C++ object you won't even get the same
Python instance for the same tree item the next time around.
"""

Instead, you should use the GetFirstChild/GetNextChild method of
treelistctrl, so if you want the very first child (of the root, I
suppose), just do this:

root = yourTreeList.GetRoot()
firstChild, cookie = yourTreeList.GetFirstChild(root)


Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/
http://thedoomedcity.blogspot.com/

werner

unread,
Nov 27, 2009, 11:10:04 AM11/27/09
to wxpytho...@googlegroups.com
Andrea,

...
> root = yourTreeList.GetRoot()
> firstChild, cookie = yourTreeList.GetFirstChild(root)
>
>
This put me on the right track.

Thanks
Werner

Following just for the archive if anyone else is running into this. I
wanted to get to the first item where itemData[2] is not None (i.e. has
an imageKey in my case).

root = tree.GetRootItem()
itemId, itemCookie = tree.GetFirstChild(root)
if itemId.IsOk():
itemData = tree.GetItemPyData(itemId)
else:
itemData = (None, None, None)

while itemData[2] == None:
itemId = tree.GetNext(itemId)
if itemId.IsOk():
itemData = tree.GetItemPyData(itemId)
else:
itemData = (None, None, None)
break

if itemData[2]:
tree.SelectItem(itemId, itemId, False)
self.ShowImage(itemData[2])


Steven Sproat

unread,
Nov 27, 2009, 11:14:07 AM11/27/09
to wxpytho...@googlegroups.com
But what about when you want to insert an item into node, say, #6? How
do you do that without a reference to its TreeItemId -- iterate over the
whole tree, counting for the node you're looking for?

--
Steven Sproat, BSc
http://www.basicrpg.com/

Andrea Gavana

unread,
Nov 27, 2009, 2:05:37 PM11/27/09
to wxpytho...@googlegroups.com
Hi,

2009/11/27 Steven Sproat:
This is one of the reason why I swapped all the instances of
wx.TreeCtrl with CustomTreeCtrl and TreeListCtrl with HyperTreeList in
my proprietary applications. There might be an easy way to do what you
ask, but if there is, I have long forgotten it.

CustomTreeCtrl and HyperTreeList might be slightly slower than their
C++ counterparts (mainly due to the fact that they are pure-Python and
Python is notoriously slower in function calls and for/while loops),
but using them the improvement in "programmability" is enormous and I
have never looked back since then. Moreover, my very first tests with
the first (unoptimized) version of CustomTreeCtrl revealed that it is
close to 5% slower than wx.TreeCtrl, and I usually don't bother about
these things in UI elements, only in heavy number crunching
procedures, but for those I always fall back to numpy (good), scipy
(less good) and Fortran via f2py (super good).

Robin Dunn

unread,
Nov 30, 2009, 2:58:15 AM11/30/09
to wxpytho...@googlegroups.com
On 11/27/09 1:50 AM, Andrea Gavana wrote:

>> When you call AddRoot/AppendItem/InsertItem/InsertItemBefore, it returns
>> a TreeItemId. Keep a reference to these (a list is nice, I find) and
>> then pass whichever ItemId you want to GetFirstChild
>
> Don't do this unless you are using HyperTreeList.
>
> """
> The wx.TreeItemId is a temporary opaque object that can *only* be used
> for passing to/from treectl methods. It has no other utility, and due
> to it's nature as a proxy to a C++ object you won't even get the same
> Python instance for the same tree item the next time around.
> """

It should be okay to hold on to a wx.TreeItemId, as long as you don't
try to use it after that node in the treectrl has been deleted or moved.

--
Robin Dunn
Software Craftsman
http://wxPython.org

Reply all
Reply to author
Forward
0 new messages