17.8. Layout Management
In all examples so far, we have made widgets visible via method pack. This is typical of real-life Tkinter usage. However, two other layout managers are sometimes useful. This section covers all three layout managers provided by Tkinter: the packer, gridder, and placer. Never mix layout managers for the same container widget: all children of a given container widget must be handled by the same layout manager, or very strange effects (including Tkinter going into infinite loops) may result.
17.8.1. The Packer
Calling method pack on a widget delegates widget layout management to a simple, flexible layout manager known as the Packer. The Packer sizes and positions widgets within a container (parent) widget according to each widget's space needs (including padx and pady). Each widget w supplies the following Packer-related methods.
Delegates layout management to the packer. pack_options may include:
When true, w expands to fill any space not otherwise used in w's parent.
Determines whether w fills any extra space allocated to it by the packer or keeps its own minimal dimensions: NONE (default), X (fill only horizontally), Y (fill only vertically), or BOTH (fill both horizontally and vertically).
Determines which side of the parent w packs against: TOP (default), BOTTOM, LEFT, or RIGHT. To avoid confusion, don't mix different values for option side= in widgets that are children of the same container. When more than one child requests the same side (for example, TOP), the rule is first come, first served: the first child packs at the top, the second child packs second from the top, and so on.
The packer forgets about w. w remains alive but becomes invisible; you may show w again later by calling w.pack again (or, perhaps, w.grid or w.place).
Returns a dictionary with the current pack_options of w.
17.8.2. The Gridder
Calling method grid on a widget delegates widget layout management to a specialized layout manager called the Gridder. The Gridder sizes and positions each widget into cells of a table (grid) within a container (parent) widget. Each widget w supplies the following Gridder-related methods.
Delegates layout management to the gridder. grid_options may include:
The column to put w in; the default is 0 (leftmost column).
How many columns w occupies; the default is 1.
How many pixels to pad w, horizontally and vertically, inside w's borders.
How many pixels to pad w, horizontally and vertically, outside w's borders.
The row to put w in; the default is the first row that is still empty.
How many rows w occupies; the default is 1.
What to do if the cell is larger than w. By default, with sticky='', w is centered in its cell. sticky may be the string concatenation of zero or more of N, E, S, W, NE, NW, SE, and SW, which are compass directions that indicate the sides and corners of the cell to which w sticks. For example, sticky=N means that w sticks to the cell's top and is centered horizontally, while sticky=N+S means that w expands vertically to fill the cell and is centered horizontally.
import Tkinter root = Tkinter.Tk( )
for r in range(3):
for c in range(4):
displays 12 labels arrayed in a 3 x 4 grid.
The gridder forgets about w. w remains alive but becomes invisible; you may show w again later by calling w.grid again (or, perhaps, w.pack or w.place).
Returns a dictionary with the current grid_options of w.
17.8.3. The Placer
Calling method place on a widget explicitly handles widget layout management thanks to a simple layout manager called the Placer. The Placer sizes and positions each widget w within a container (parent) widget exactly as w explicitly requires. Other layout managers are usually preferable, but the Placer can help you implement custom layout managers. Each widget w supplies the following Placer-related methods.
Delegates layout management to the placer. place_options may include:
The exact spot of w other options refer to: N, E, S, W, NE, NW, SE, or SW, which are compass directions that indicate the corners and sides of w; the default is NW (upper-left corner of w)
INSIDE (the default) to indicate that other options refer to the parent's inside (ignoring the parent's border); OUTSIDE otherwise
Height and width in pixels
Height and width as a float between 0.0 and 1.0, and as a fraction of the height and width of the parent widget
Horizontal and vertical offset as a float between 0.0 and 1.0, and as a fraction of the height and width of the parent widget
Horizontal and vertical offset in pixels
The placer forgets about w. w remains alive but becomes invisible; you may show w again later by calling w.place again (or, perhaps, w.pack or w.grid).
Returns a dictionary with the current place_options of w.