4.0.0a3 wx.EVT_TREE_ITEM_ACTIVATED event not getting TreeItemID

34 views
Skip to first unread message

Allan C.

unread,
Jul 21, 2017, 2:55:08 AM7/21/17
to wxPython-users
Hi. I have the tree event binded:

class MyFrame(...)
    def __init__(...)
        ...
        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.on_tree_item_activated, self.tree_ctrl)
        ...

    def on_tree_item_activated(self, event):
        LOGGER.info(event.GetItem())
        LOGGER.info(event.GetItem().GetID())
        LOGGER.info(type(event.GetItem().GetID()))
        LOGGER.info(self.tree_ctrl_side_menu.GetSelection())

Every nodes when on double clicked/activated returns the same object in the memory space. "event.GetItem().GetID()" returns a "sip.voidptr object" type. Is there a way to know which tree item is activated?

Tim Roberts

unread,
Jul 21, 2017, 2:36:38 PM7/21/17
to wxpytho...@googlegroups.com
What you have there is a "TreeItemId", like a handle for the item. To
get the text for the item, use:
text = self.tree_ctrl.GetItemText( event.GetItem() )

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Allan C.

unread,
Jul 21, 2017, 8:13:58 PM7/21/17
to wxpytho...@googlegroups.com
Tim Roberts wrote:
> What you have there is a "TreeItemId", like a handle for the item. To
> get the text for the item, use:
> text = self.tree_ctrl.GetItemText( event.GetItem() )

This works! I thought I have tried this. Thanks. Nonetheless, just
wondering if I can do something like this as well?

UI:
self.tree_ctrl = wx.TreeCtrl(self.panel, wx.ID_ANY, style=wx.TR_HIDE_ROOT)
self.tree_root = self.tree_ctrl.AddRoot('ROOT')
self.tree_leaf = self.tree_ctrl.AppendItem(self.tree_root, 'LEAF')

Event:
if event.GetItem().GetID() == self.tree_leaf.GetID()

Allan C.

unread,
Jul 21, 2017, 8:46:15 PM7/21/17
to wxpytho...@googlegroups.com
I think comparing the ID could be a better approach, since string
comparison will not let me know which item is activated if there are
items with the same text but on different branches in the TreeCtrl. I
could format the text to make it unique on each leaf nodes - just
exploring some options. Appreciate your help. Cheers.

Allan C.

unread,
Jul 21, 2017, 9:43:09 PM7/21/17
to wxpytho...@googlegroups.com
Please ignore the spam. Got it working with the following:

selection = self.tree_ctrl.GetSelection()
if selection == self.tree_node_id:
Reply all
Reply to author
Forward
0 new messages