Page 70 - Programming the Raspberry Pi Getting Started with Python
P. 70
the application, starting with a window with the title “Temp Converter” and a single label:
We have added a class to the program called App. It has an __init__ method that is used when a
new instance of App is created in the following line:
We pass in the Tk root object to __init__ where the user interface is constructed.
As with the Hello World example, we are using a Label, but this time rather than adding the label
to the root Tk object, we add the label to a Frame object that contains the label and other items that
will eventually make up the window for our application. The structure of the user interface is shown in
Figure 7-3. Eventually, it will have all the elements shown.
Figure 7-3 Structure of the user interface
The frame is “packed” into the root, but this time when we add the label, we use the method grid
instead of pack. This allows us to specify a grid layout for the parts of our user interface. The field
goes at position 0, 0 of the grid, and the button object that is created on the subsequent line is put on
the second row of the grid (row 1). The button definition also specifies a “command” to be run when
the button is clicked. At the moment, this is just a stub that prints the message “Not implemented.”
The function wm_title sets the title of the window. Figure 7-4 shows what the basic user interface
looks like at this point.