wx.TreeCtrl: Current Selection Text

How do I get the text of the current selection of a wx.TreeCtrl widget?

The following code gives me an error:

self.tree.GetItemText(self.tree.GetSelection())

File “C:\Python27\lib\site-packages\wx-2.9.4-msw\wx_controls.py”, line 5262, in GetItemText

return controls.TreeCtrl_GetItemText(*args, **kwargs)

PyAssertionError: C++ assertion “item.IsOk()” failed at …\src\msw\treectrl.cpp(949) in wxTreeCtrl::GetItemText(): invalid tree item

Thanks.

Austin

Hi,

···

On Thursday, August 8, 2013 5:49:41 PM UTC-5, austin aigbe wrote:

How do I get the text of the current selection of a wx.TreeCtrl widget?

The following code gives me an error:

self.tree.GetItemText(self.tree.GetSelection())

File “C:\Python27\lib\site-packages\wx-2.9.4-msw\wx_controls.py”, line 5262, in GetItemText

return controls.TreeCtrl_GetItemText(*args, **kwargs)

PyAssertionError: C++ assertion “item.IsOk()” failed at …..\src\msw\treectrl.cpp(949) in wxTreeCtrl::GetItemText(): invalid tree item

Thanks.

Austin

Looking at the wxPython demo, it appears that all you need to do is catch the selection event and do the following:

self.item = event.GetItem()
print self.tree.GetItemText(self.item)

Hope that helps!
Mike

austin aigbe wrote:

How do I get the text of the current selection of a wx.TreeCtrl widget?

The following code gives me an error:

self.tree.GetItemText(self.tree.GetSelection())

File "C:\Python27\lib\site-packages\wx-2.9.4-msw\wx\_controls.py", line
5262, in GetItemText
return _controls_.TreeCtrl_GetItemText(*args, **kwargs)
PyAssertionError: C++ assertion "item.IsOk()" failed at
..\..\src\msw\treectrl.cpp(949) in wxTreeCtrl::GetItemText(): invalid
tree item

Are you sure that there is a tree item selected when that code is run? You should be able to check if the item is valid with an if statement since the class has a __nonzero__ method. Something like this:

     item = self.tree.GetSelection()
     if item:
         text = self.tree.GetItemText(item)
         # Do something with text

···

--
Robin Dunn
Software Craftsman