window.
LISTING 18.2 The script1802.py File
Click here to view code image
#!/usr/bin/python3
from tkinter import *
class Application(Frame):
"""Build the basic window frame template"""
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
root = Tk()
root.title('Test Application window')
root.geometry('300x100')
app = Application(root)
app.mainloop()
When you run the script1802.py program, you might notice that it looks just like the window you
created using the bare Tk object, shown in Figure 18.1. The difference is that now the window has a
frame, so you can start adding widgets to the Application object to fill in the window. The
script1802.py code shows the basic template that you’ll use for most of your Python GUI
programs.
Positioning Widgets
The key to a user-friendly GUI application is the placement of the widgets in the window area. Too
many widgets grouped together can make the user interface confusing.
In the example in the previous section, you used the grid() method to position widgets in the frame.
The tkinter package provides three ways to position widgets in the window:
Using a grid system
Packing widgets into available places
Using positional values
The last method, using positional values, requires that you define the precise location of each widget,
using X and Y coordinates within the window. While this provides the most accurate control over
where your widgets appear, it can be somewhat difficult to work with when you’re first starting out.
The packing method pretty much does what it says: It attempts to pack widgets into a window as best
it can in the space available. When you choose this method, Python places the widgets in the window
for you, starting at the top left and moving along to the next available space, either to the right or
below the previous widget. The packing method works fine for small windows with just a few
widgets, but if you have a larger window, things can quickly get cluttered and out of alignment.
The compromise between the positional method and the packing method is the grid method. The grid
method creates a grid system in the window, using rows and columns, somewhat like a spreadsheet.
You place each widget in the window at a specific row and column location. You can define a widget