wx.Dialog doesn't destroy when expected

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

wx.Dialog doesn't destroy when expected

Andy Kelts

I have a dialog that has some advanced features on it, including pushing event handlers. When the dialog shuts down, I need to pop the event handlers, but the code to pop them is never triggered, as the dialog is never closed or destroyed.

To demonstrate this, I made a simpler dialog and found that once created, the dialog is never destroyed until the entire app gets shut down. I’ve provided sample code that demonstrates this. The results of running this code and clicking [OK] are:

ok

done

on destroy

 

I would expect:

ok

on destroy

done

 

I’ve also tried the following code for creation/destruction of the dialog with identical results:

if __name__ == '__main__':

       app = wx.App( )

       dlg = My_Dialog( None )

       result = dlg.ShowModal( )

       if result == wx.ID_OK:

              print( 'ok' )

       dlg.Destroy( )

 

       print( 'done' )

 

Is there something I’m missing about how I should be going about catching the destruction or close events of a dialog?

Sample code (also attached):

 

import wx

 

class My_Dialog( wx.Dialog ):

       def __init__( self, parent ):

              super( ).__init__( parent )

 

              szr_main = wx.BoxSizer( wx.VERTICAL )

              self.txt_control = wx.TextCtrl( self, value = 'Sample Text' )

              szr_main.Add( self.txt_control, 1, wx.EXPAND | wx.ALL, 5 )

 

              button_bar = wx.StdDialogButtonSizer( )

              self.button_bar_ok = wx.Button( self, wx.ID_OK )

              button_bar.AddButton( self.button_bar_ok )

              self.button_bar_cancel = wx.Button( self, wx.ID_CANCEL )

              button_bar.AddButton( self.button_bar_cancel )

              button_bar.Realize( )

              szr_main.Add( button_bar, 0, wx.EXPAND | wx.ALL, 5 )

 

              self.SetSizer( szr_main )

              self.Layout( )

              self.Centre( wx.BOTH )

 

              self.Bind( wx.EVT_BUTTON, self._on_button )

              self.Bind( wx.EVT_WINDOW_DESTROY, self._on_destroy )

              self.Bind( wx.EVT_CLOSE, self._on_close )

 

 

       def _on_button( self, event ):

              self.EndModal( event.GetId( ) )

 

 

       def _on_close( self, event ):

              print( 'on close' )

              event.Skip( )

 

 

       def _on_destroy( self, event ):

              print( 'on destroy' )

              event.Skip( )

 

 

if __name__ == '__main__':

       app = wx.App( )

       with My_Dialog( None ) as dlg:

              result = dlg.ShowModal( )

              if result == wx.ID_OK:

                     print( 'ok' )

 

       print( 'done' )




This e-mail may contain CONFIDENTIAL AND PROPRIETARY INFORMATION and/or PRIVILEGED AND CONFIDENTIAL COMMUNICATION intended solely for the recipient and, therefore, may not be retransmitted to any party outside of the recipient's organization without the prior written consent of the sender. If you have received this e-mail in error please notify the sender immediately by telephone or reply e-mail and destroy the original message without making a copy. Deep Silver, Inc. accepts no liability for any losses or damages resulting from infected e-mail transmissions and viruses in e-mail attachments.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

dialog.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: wx.Dialog doesn't destroy when expected

Robin Dunn
Andy Kelts wrote:

I have a dialog that has some advanced features on it, including pushing event handlers. When the dialog shuts down, I need to pop the event handlers, but the code to pop them is never triggered, as the dialog is never closed or destroyed.

To demonstrate this, I made a simpler dialog and found that once created, the dialog is never destroyed until the entire app gets shut down. I’ve provided sample code that demonstrates this. The results of running this code and clicking [OK] are:

ok

done

on destroy

 

I would expect:

ok

on destroy

done

 

I’ve also tried the following code for creation/destruction of the dialog with identical results:

if __name__ == '__main__':

       app = wx.App( )

       dlg = My_Dialog( None )

       result = dlg.ShowModal( )

       if result == wx.ID_OK:

              print( 'ok' )

       dlg.Destroy( )

 

       print( 'done' )

 

Is there something I’m missing about how I should be going about catching the destruction or close events of a dialog?


This is by design. When the Destroy method of top-level windows is called, it doesn't actually do the destroy immediately but adds it to a list of things that will be destroyed later. This is done to help ensure that there are no pending events waiting for the destroyed window, that would likely cause a crash if wx tried to process those events after the dialog is gone. If your sample application had other windows and the main loop running then you would likely see the "on destroy" message sometime shortly after control returns to the main loop.


--
Robin Dunn
Software Craftsman
http://wxPython.org

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.