Search code examples
wxpython

When does a wxPython modeless frame created at the local scope get actually destroyed?


I have a wxPython application in which I bound one wx.EVT_MENU event on my main frame to a callback that pops up a child wxFrame (I want the popped up window to be modeless whence the use of a wxFrame in that case). My callback looks like:

def on_open_my_frame(self,event):
    local_frame = MyFrame(self)
    local_frame.Show()

This code is functional in the sense that the event actually pops up a frame I can interact with. However, I am a bit surprised that it works. Indeed, due to the very nature of my modeless frame I would expect the garbage collector to act upon my local_frame once out of the scope of on_open_my_frame method. Is that code valid ? Or in other words is there some kind of mechanism in wxPython that prevents a window to get destroyed until it is displayed ?


Solution

  • GUI frameworks work a bit differently then other applications in that they are event based where the event loop runs continuously. Thus when you start up a frame and show it, the frame just hangs around waiting for the user to do something with it. In other words, the widgets you create wait for the user to interact with them, thus generating an event.

    The only way for the frame to exit is if you tell the frame instance you created to explicitly Close() or Destroy() itself. Otherwise the frame will wait for the user to close it or interact with it in some other manner.