Understanding the application object lifecycle

The lifecycle of your wxPython application object begins when the application instance is created and ends just after the last application window is closed. This does not necessarily correspond to the beginning and ending of the Python script that surrounds your wxPython application. The script may choose to do some activity before creating the wxPython application, and may do further cleanup after the application MainLoop() exits. All wxPython activity, however, must be performed during the life of the application object. As we've mentioned, this means that your main frame object cannot be created until after the wx.App object is created. (This is one reason why we recommend creating the top-level frame in the OnInit() method—doing so guarantees that the application already exists.)

As figure 2.2 shows, creating the application object triggers the OnInit() method and allows new window objects to be created. After OnInit(), the script calls MainLoop(), signifying that wxPython events are now being handled. The application continues on its merry way, handling events until the windows are closed. After all top-level windows are closed, the MainLoop() function returns to the calling scope and the application object is destroyed. After that, the script can close any other connections or threads that might exist.

Automatically calls Onlnit() method

Events are now processed

Automatically calls Onlnit() method

This happens after all top-level windows are closed

At this point, window objects can be created

This happens after all top-level windows are closed

Figure 2.2 Major events in the wxPython application lifecycle, including the beginning and ending of both the wxPython application and the script which surrounds it

One reason to be aware of the main application life cycle is that, while active, a wxPython application will take control of certain system functions, such as the standard output streams. We'll discuss how to direct output in the next section

Was this article helpful?

0 0

Post a comment