Adding Window Components

Window components, or widgets as they are sometimes called, are added to an application window by calling specific methods on self within the application classÕs __init__ method.  Each such method

creates a widget with some default attributes and some attributes provided by the methodÕs arguments

places the widget in the windowÕs grid


returns the widget to the application for further access and manipulation 

The methods and the arguments used to add the widgets to the window of the tax calculator are shown below.


Type of Window component

Method to add it to the window


addLabel(text, row, column,

         columnspan = 1, rowspan = 1,

         sticky = N+W, font = None)


addButton(text, row, column,

          columnspan = 1, rowspan = 1,

          command = lambda: None,

          state = NORMAL)


addFloatField(value, row, column,

              columnspan = 1, rowspan = 1,

              width = 20, sticky = N+E,

              precision = None)


addIntegerField(value, row, column,

                columnspan = 1, rowspan = 1,

                width = 10, sticky = N+E)


Note that the required arguments (those without default values) to these methods include an initial value to be displayed in the widget, as well the row and column where the widget is to go in the windowÕs grid. 

The rows and columns in the grid are numbered from 0.  Thus, the grid position (0, 0) would lie in the upper left corner of the window.  The tax calculator windowÕs grid has four rows of two columns each, as shown here:


The next code segment, from the tax calculator program, adds label, float field, and button widgets to the window:

# Label and field for the income

self.addLabel(text = "Income",

              row = 0, column = 0)

self.incomeField = self.addFloatField(value = 0.0,

                                      row = 0,

                                      column = 1)


# The command button

self.addButton(text = "Compute", row = 3, column = 0,

               columnspan = 2, command = self.computeTax)



In this application, each widget except for the command button occupies a single column and a single row in the grid.  The command button occupies both columns in its row, because the programmer has specified its columnspan attribute as 2.

Along with a grid position and extent, a widget also has a sticky attribute.  This attribute indicates the alignment of a widget within its grid cell, when the cell area is larger than the widget itself.  The default values of this attribute are NW and NE for labels and entry fields, respectively, because a label is often placed to the left of a field.  But these alignments can be overridden when the widgets are added to a window.

Each type of entry field has a default width attribute.  This represents the number of characters that will be visible within the field.  The float field also has a precision attribute allowing the programmer to specify the number of places to display to the right of a numberÕs decimal point. The label has a font attribute and the button has command and state attributes.  The roles of these attributes will become clear in later examples.

Note that the example program saves a reference (self.incomeField) to the widget returned by the method addFloatField, whereas the methods addButton and addLabel are run simply to create and add widgets to the window.  Generally, the __init__ method must establish such references only for widgets that will be used later in the program code.  In this case, the field will be accessed for input data in the programÕs event handler method.