A wxPython application does not have a main procedure; the equivalentis thewx.AppConsole.OnInit
member defined for a class derivedfrom wx.App.
OnInit will usually create a top window as a bare minimum. Unlike inearlier versions of wxPython,OnInit does not return a frame.Instead it returns a boolean value which indicates whether processingshould continue (True
) or not (False
).
An application closes by destroying all windows. Because all framesmust be destroyed for the application to exit, it is advisable to useparent frames wherever possible when creating new frames, so thatdeleting the top level frame will automatically delete child frames.The alternative is to explicitly delete child frames in the top-levelframe’s wx.CloseEvent handler.
In emergencies thewx.Exit
function can be called to kill theapplication however, normally the application shuts down automaticallywhen the last top-level window closes. SeeApplication Shutdown.
An example of defining an application follows:
classDerivedApp(wx.App):defOnInit(self):the_frame=wx.Frame(None,-1)# Other initialization code...the_frame.Show(True)returnTrue
The application normally shuts down when the last of its top levelwindows is closed. This is normally the expected behaviour and meansthat it is enough to callwx.Window.Close
in response to the“Exit” menu command if your program has a single top level window. Ifthis behavior is not desirablewx.PyApp.SetExitOnFrameDelete
canbe called to change it.
Note
Note that such logic doesn’t apply for the windows shownbefore the program enters the main loop: in other words, you cansafely show a dialog fromwx.AppConsole.OnInit
and not beafraid that your application terminates when this dialog – whichis the last top level window for the moment – is closed.
Another aspect of the application shutdown iswx.AppConsole.OnExit
which is called when the applicationexits but before wxPython cleans up its internal structures.