Next / Previous / Contents

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.

  • A cell is the area at the intersection of one row and one column.

  • The width of each column is the width of the widest cell in that column.

  • The height of each row is the height of the largest cell in that row.

  • For widgets that do not fill the entire cell, you can specify what happens to the extra space. You can either leave the extra space outside the widget, or stretch the widget to fit it, in either the horizontal or vertical dimension.

  • You can combine multiple cells into one larger area, a process called spanning.

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.