wx.Dialog doesn't destroy when expected

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’ )

dialog.py (1.21 KB)

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?