[wxPython-users] Closing window with X button fires multiple EVT_TREE_SEL_CHANGED events

Robin Dunn robin at alldunn.com
Tue Oct 30 11:29:12 PDT 2007


Larry Bates wrote:
> I ran into a problem when enabling/disabling menu functions based on 
> folders that are selected in a tree control with virtual mixin.  I bound 
> a method to EVT_TREE_SEL_CHANGED, but when user clicks X button to close 
> the window the method gets called n-1 times where n is the number of 
> folders that are displayed in the top level less 1. 

This is a quirk of the native tree widget on Windows.  When it is being 
destroyed it removes the items one at a time, and if the one being 
removed is selected then it selects another and sends the events.


> Error that is 
> returned is:
> 
> Traceback (most recent call last):
>   File "wsupload.py", line 462, in onSelectionChanged
>     self.frame.setMenuState()
>   File "wsupload.py", line 2317, in setMenuState
>     self.myMenu.Upload.Enable(state)
>   File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", 
> line 1404
> 5, in __getattr__
>     raise PyDeadObjectError(self.attrStr % self._name)
> wx._core.PyDeadObjectError: The C++ part of the MyMenuBar object has 
> been delete
> d, attribute access no longer allowed.
> 
> This is because onSelectionChanged gets called because 
> EVT_TREE_SEL_CHANGED is bound to it.  It calls setMenuState, but the 
> menu has apparently been destroyed by that time.  The only workaround 
> seems to be:
> 
>     try:
>         self.myMenu.Upload.Enable(state)
> 
>     except wx._core.PyDeadObjectError: # Workaround for exit button
>         pass
> 
> But that seems HIGHLY ugly and potentially dangerous.

Put this at the beginning of your event handler:

	if self.IsBeingDeleted():
		return.

-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list