Thursday, October 23, 2008

How do I shut down my wxPython application?

When the last top-level window in your application is closed by the user,the wxPython application exists.By top-level window ,it's not just the frame designated using SetTopWindow()method,it mean any frame without parents.To trigger a shutdown programtically,you can call Close() on all top-level window.
During the shutdown process,wxPython takes care of deleting all its windows and freeing their resources.Sometimes you want to perform your own cleanup ,for example close db conection,close file resources and so on,then you have one hook when application exists,you can override the OnExist() method to do you own process. Even if the application is closed by wx.Exist(),the OnExist method is still triggered.
If for somereasons you want to your application continue to run after last window closes,you can change the default behavious using the method of wx.App,SetExistOnFrameDelete(flag).If the flag is False the application will continue to run even if the last window of you application is closed.This means the wx.App instance will continue to live,and event loop will continue to process events. The application will remain untill the method wx.Exist invoke.
You can close your application in controlled way sometimes,maybe you need to end your application immediately and don't care that your program cleans up itself fully,for example a critical resource may have close.In this situation ,you can call the method of wx.App ,it's ExistMainLoop(),this method explicitily causes the main message loop to break,causing the control to leave the MainLoop().This will generally end the application.

No comments: