1. Home
  2. Computing & Technology
  3. Python

An Introduction to Programming wxPython

From Al Lukaszewski, for About.com

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!'.

Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.