1. Computing

"Hello, World!" in Python With Tkinter

By

5 of 10

Me, Myself, and I

Within the Greeter class, we next need to define the __init__ function. It is called __init__ because it is called whenever an object instance of the class is instantiated. Without it, you will have the dream of an object but will never be able to realise it (you won't be able to create one). So let's create the first function of our class as follows (mind any wrap):

      def __init__(self, master=None): 
           Frame.__init__(self, master) 
           self.pack() 
           self.createWidgets() 
 
As with most __init__ functions, we here require the argument self to represent the function itself. By default, all classes derived from Tkinter's Frame class will look for the master widget to be defined. Here we tell it has none.

Within the function itself, we then call the Frame class' __init__ class to initiate a frame in memory. Then we pack it and tell Python to create the widgets. The pack method is inherited from Frame and makes the frame visible.

The createWidgets method is defined in the next step and is necessary for the user to do anything with the program. If you want an illustration of a frame without a widget, simply comment out this line when you run the program. Don't worry, it won't go very far.

©2014 About.com. All rights reserved.