I figured out the solution to the problem described below, but I don’t understand why the solution works, so thought I would ask for some explanation, as I seem to be missing something about binding events in general.
I have a Panel (called self) with a child tree control (wx.TreeCtrl) called self.tree. This tree is embedded in a pane of an AUI Manager which manages self. I don’t think the AUI manager aspect is the issue here, but anyway, I bound the event wx.EVT_TREE_SEL_CHANGED to an event handler within self.
When I bound the event to the tree like this:
if I then triggered any of these events while the frame was open by selecting tree items, when I then closed the top level frame, I would get an error upon close “RuntimeError: wrapped C/C++ object of type TreeCtrl has been deleted”, triggered within the event handler statement created above. Somehow binding the event to the tree results in the event handler getting called after the tree has been deleted. However, if I bound the event this way directly to the panel:
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeSelection, self.tree)
everything works fine.
Why is it necessary to bind a tree event handler to the parent panel/frame rather than to the tree itself? In the second statement, I still specified self.tree as the source of the event, but by binding it to the Panel (self), the event handler functions properly. This confuses me because I have often bound event handlers for a button directly to the button (wx.Button.Bind(wx.EVT…, event handler)) without any trouble. Not sure why you can’t do the same with a tree control.