1. Computing

An Introduction to Programming wxPython

From , former About.com Guide

4 of 6

OnInit: The Moment of Instantiation

After defining the Greeter class as a type of wx.App, we define actions that should happen when the class is instantiated as an object. For this we use the function OnInit.

 def OnInit(self): 
 frame = wx.Frame(None, -1, "Hello, World!") 

When the Greeter object is instantiated, an instance of wx.Frame will be created. The object name in our program is frame. wx.Frame is a constructor (a function that is called when an object is created) and functions as a proxy to a C++ Frame class in the background.

Within wxPython, wx.Frame is a container widget - it contains other widgets. While it has a bevy of functionality that goes with it (run 'help(wx.Frame)' in a Python shell for documentation), wx.Frame requires only three arguments in the following order: parent, id, and title.

The parent is the parent window in which the frame should appear. For our purposes, there is none. So we declare it as 'None'.

The id is the identifier used to reference the frame object. We are only using one window, so we identify it as '-1'.

Finally, the window needs a title. Our simple window will bear the title 'Hello, World!'.

  1. About.com
  2. Computing
  3. Python
  4. Advanced Python
  5. Python Graphic Interface - Import wxPython - Programming wxPython - Python GUI Programming - OnInit

©2013 About.com. All rights reserved.