Creating a wxApp subclass

Creating your own subclass of wx.App is so simple that it's often a good idea to create one when you start your application even if you don't need any custom functionality. That way, you'll have the subclass if you need it later. To create and use a wx.App subclass, you need to perform four steps:

1 Define the subclass.

2 Write an OnInit() method within the subclass definition.

3 Create an instance of the class in the main section of your program.

4 Call the MainLoop() method of the application instance. This method transfers program control to wxPython.

We saw the OnInit() method in chapter 1. It's called by the wxPython system when the application is started and before the main event loop begins. This method takes no parameters and returns a boolean value—if the return value is False, then the application will exit immediately. In most cases, you'll want to hardwire True as the result of this method. Exiting might be the proper way to handle certain error conditions, such as the absence of a required resource.

Because the OnInit() method exists, and is part of the wxPython framework, any initialization needed for your custom class is typically managed there, and not in the Python__init__special method. If you decide that you need an

_init_method for some reason, you must call the_init_method of the parent class in that method, as in the following.

Typically, you'll create at least one frame object within the OnInit() method, and you'll also call the Show() method of that frame. You may optionally specify that the frame is the top window for the application by calling the method SetTopWin-dow(). The top window is used as the default parent for dialogs that are created without a parent—it's essentially the main window of your program. We'll discuss the top-level window in section 2.5.

Was this article helpful?

0 0

Post a comment