[wxPython-users] Event Handling Anomaly
Robin Dunn
robin at alldunn.com
Mon Dec 3 12:13:36 PST 2007
Mark Erbaugh wrote:
>
> When you run this example, if you select an item in the wx.ListBox, you
> will get an error when you exit the app:
>
> wx._core.PyDeadObjectError: The C++ part of the StaticText object has
> been deleted, attribute access no longer allowed.
>
> You don't get the error if you just launch the app and exit without
> selecting an item in the wx.ListBox
>
> You can eliminate the error by switching the order of the two lines
> creating the wx.StaticText and the wx.ListBox.
>
> You can also eliminate the error by binding the event handler to the
> wx.Frame instead of to the wx.ListBox.
>
> It seems that if an event handler is bound to it, the wx.ListBox
> triggers the handler when it is being destroyed.
Correct. The selected item is deselected by the native widget in the
process of destroying it, so an event is sent. In your original case
the static text has already been destroyed by the time this event is
sent, so that is why switching the order of creation has an effect.
The best way to deal with situations like this is to either check in the
event handler if the widget still exists (if self.st:...) or to check if
the parent is in the process of being destroyed (if not
self.IsBeingDeleted():...) or something similar.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the wxpython-users
mailing list