Next / Previous / Contents / Shipman's homepage

4. Layout management

Later we will discuss the widgets, the building blocks of your GUI application. How do widgets get arranged in a window?

Although there are three different “geometry managers” in Tkinter, the author strongly prefers the .grid() geometry manager for pretty much everything. This manager treats every window or frame as a table—a gridwork of rows and columns.

When you create a widget, it does not appear until you register it with a geometry manager. Hence, construction and placing of a widget is a two-step process that goes something like this:

    self.thing = tk.Constructor(parent, ...)
    self.thing.grid(...)

where Constructor is one of the widget classes like Button, Frame, and so on, and parent is the parent widget in which this child widget is being constructed. All widgets have a .grid() method that you can use to tell the geometry manager where to put it.

4.1. The .grid() method

To display a widget w on your application screen:

    w.grid(option=value, ...)

This method registers a widget w with the grid geometry manager—if you don't do this, the widget will exist internally, but it will not be visible on the screen. For the options, see Table 1, “Arguments of the .grid() geometry manager”.

Table 1. Arguments of the .grid() geometry manager

column The column number where you want the widget gridded, counting from zero. The default value is zero.
columnspan Normally a widget occupies only one cell in the grid. However, you can grab multiple cells of a row and merge them into one larger cell by setting the columnspan option to the number of cells. For example, w.grid(row=0, column=2, columnspan=3) would place widget w in a cell that spans columns 2, 3, and 4 of row 0.
in_ To register w as a child of some widget w2, use in_=w2. The new parent w2 must be a descendant of the parent widget used when w was created.
ipadx Internal x padding. This dimension is added inside the widget inside its left and right sides.
ipady Internal y padding. This dimension is added inside the widget inside its top and bottom borders.
padx External x padding. This dimension is added to the left and right outside the widget.
pady External y padding. This dimension is added above and below the widget.
row The row number into which you want to insert the widget, counting from 0. The default is the next higher-numbered unoccupied row.
rowspan Normally a widget occupies only one cell in the grid. You can grab multiple adjacent cells of a column, however, by setting the rowspan option to the number of cells to grab. This option can be used in combination with the columnspan option to grab a block of cells. For example, w.grid(row=3, column=2, rowspan=4, columnspan=5) would place widget w in an area formed by merging 20 cells, with row numbers 3–6 and column numbers 2–6.
sticky This option determines how to distribute any extra space within the cell that is not taken up by the widget at its natural size. See below.

  • If you do not provide a sticky attribute, the default behavior is to center the widget in the cell.

  • You can position the widget in a corner of the cell by using sticky=tk.NE (top right), tk.SE (bottom right), tk.SW (bottom left), or tk.NW (top left).

  • You can position the widget centered against one side of the cell by using sticky=tk.N (top center), tk.E (right center), tk.S (bottom center), or tk.W (left center).

  • Use sticky=tk.N+tk.S to stretch the widget vertically but leave it centered horizontally.

  • Use sticky=tk.E+tk.W to stretch it horizontally but leave it centered vertically.

  • Use sticky=tk.N+tk.E+tk.S+tk.W to stretch the widget both horizontally and vertically to fill the cell.

  • The other combinations will also work. For example, sticky=tk.N+tk.S+tk.W will stretch the widget vertically and place it against the west (left) wall.