The application is supposed to close when I click the close button for the main frame. But the way I implemented it, it quits with a Segmentation fault when I c
I am not sure why do you need the callback to close the application? If you do not bind anything to the MainFrame, the application should automatically be closed when you click the close button. Unless you have some other thread running which prevents the process to end. If you still want to bind to the close event and do something before closing you should use event.Skip() in the event handler if you still want to close the window. Otherwise the event is not propagated further and default handler is not executed. An example:
import wx
class MainFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.__close_callback = None
self.Bind(wx.EVT_CLOSE, self._when_closed)
def register_close_callback(self, callback):
self.__close_callback = callback
def _when_closed(self, event):
doClose = True if not self.__close_callback else self.__close_callback()
if doClose:
event.Skip()
if __name__ == "__main__":
app = wx.App(False)
mf = MainFrame(None, title='Test')
mf.Show()
mf.register_close_callback(lambda: True)
app.MainLoop()
When you execute the code and click close - the application is closed. When you change the callback function to lambda: False the window will not be closed since Skip is not called.