[wxPython-users] identify/track an item in a TreeCtrl
Robin Dunn
robin at alldunn.com
Mon Dec 3 12:13:06 PST 2007
Andrea Gavana wrote:
> Hi Harry,
>
> On Dec 3, 2007 10:59 AM, Pan Xingzhi wrote:
>> Is there a way to identify/track an item in a TreeCtrl? This action
>> typically takes place in an event handler where the exact "source" of the
>> event is needed.
>> I tried event.GetItem(), but the returned TreeItemId instance is not the
>> one returned by AppendItem. However their m_pItem property looks the same.
>> Can I use that?
>> I read the source code in the wxPython demo. It just retrieves the text
>> of the item using GetItemText(event.GetItem()). Though the TreeItemId
>> instance is changed, we can still get the same information (text, color...)
>> of the item. There must be some magic behind scene but I'm just confused.
>
> The TreeItemId generated by the C++ wxWidgets code is an
> evanescent/ghostly entity which may or may not change depending on
> what you do with the TreeCtrl.
Correct. Essentially, the wx.TreeItemId class is intended as just a
temporary handle for the item, which is created and destroyed as needed.
You can hold on to TreeItemId instances and use them later, but as
you've seen a new instance is created whenever a new item is returned
from wx.
> Using event.GetItem() will almost
> surely not returns the same TreeItemId as AppendItem.
It *never* will. A new handle will always be created for the GetItem
return value.
> Depending on
> which are your needs, you might only use the item returned by
> event.GetItem(), or store some particular identifier into the item
> PyData (using SetPyData/GetPyData),
That is what I would suggest.
> or maybe use the m_pIten property
> (but I never used it and I don't even know what it's for).
Although it is public, personally I would just consider it an
implementation detail and would shy away from using it like this. The
C++ usage of it may not be very likely to change, but the way that SWIG
is creating access wrappers to that type of value may.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the wxpython-users
mailing list