ICS H32 Fall 2024
Notes and Examples: Tkinter Layout
Background
When we build applications with graphical user interfaces that are made up of many widgets — many buttons, text entry fields, canvases, menus, etc. — one of the problems we face is the problem of layout, which is fundamentally the problem of deciding where the various widgets will appear within their window. Tkinter provides tools called layout managers that solve this problem in a few common ways, along with ways to create one's own layout manager if the built-in tools don't do exactly what you need.
Of the built-in layout managers included in Tkinter, the most flexible is one called grid
, which should address most straightforward GUI layout needs. This code example focuses on the details of how grid
works.
The grid layout manager
The grid
layout manager arranges widgets by placing each one into one or more cells on an invisible grid. The grid cells themselves are arranged by the layout manager into rows and columns, with the widgets then arranged within the cells. When you create a GUI using the grid
layout manager, you specify which widgets will appear in which cells, as well as the rules dictating how the cells' sizes change as the window's size changes, and how the widgets' sizes and positions change as the grid cells' sizes change.
There are a few things to know about a grid
-based layout.
sticky
. If a widget is placed into a particular grid cell, the sticky
value for a widget specifies which sides of the cell it "sticks" to. The sticky
value is specified by adding together the directions "north", "south", "east", and "west".
padx
, and vertical padding using pady
.rowspan
and/or columnspan
options. For example, if a widget is specified to be in row
5 and column
4 with rowspan
3 and columnspan
2, it will span three rows starting from row 5 (i.e., rows 5, 6, and 7) and two columns starting from column 4 (i.e., columns 4 and 5).grid
layout can be applied to the entire contents of a window; it can also be applied to the contents of a Frame
widget, a widget whose job is to contain other widgets. This allows the entire contents of a window to be laid out in one way, but the widgets in a smaller area of the window to be laid out differently.A more complex example
In this example, we highlight an example GUI that demonstrates how to control the layout of widgets using Tkinter's grid layout. The image below shows what the GUI looked like when we were done, with yellow gridlines drawn over the GUI so you can see where each grid cell ends and the next one begins.
A few things are evident from the example:
sticky
option. For example, Button 1
is centered vertically along the left edge of its grid cell because its sticky
option has the value tkinter.W
(west), while Button 2
rides along the lower right corner of its grid cell because its sticky
option is tkinter.S + tkinter.E
(southeast).Canvas
widget spans two columns, so that it will lie below both buttons. (There would have been other ways to accomplish this, such as using a Frame
widget to lay out Button 1
and Button 2
.)
Frame
widget with its own separate grid
layout. The Frame
widget spans two rows of the window's grid.