Previous Page
Next Page

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.

pack

w.pack(**pack_options)

Delegates layout management to the packer. pack_options may include:


expand

When true, w expands to fill any space not otherwise used in w's parent.


fill

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).


side

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.

pack_forget

w.pack_forget( )

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).

pack_info

w.pack_info( )

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.

grid

w.grid(**grid_options)

Delegates layout management to the gridder. grid_options may include:


column

The column to put w in; the default is 0 (leftmost column).


columnspan

How many columns w occupies; the default is 1.


ipadx, ipady

How many pixels to pad w, horizontally and vertically, inside w's borders.


padx, pady

How many pixels to pad w, horizontally and vertically, outside w's borders.


row

The row to put w in; the default is the first row that is still empty.


rowspan

How many rows w occupies; the default is 1.


sticky

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.


For example:

import Tkinter root = Tkinter.Tk( )
for r in range(3):
    for c in range(4):
        Tkinter.Label(root, text='R%s/C%s'%(r,c),
            borderwidth=1 ).grid(row=r,column=c)
root.mainloop( )

displays 12 labels arrayed in a 3 x 4 grid.

grid_forget

w.grid_forget( )

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).

grid_info

w.grid_info( )

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.

place

w.place(**place_options)

Delegates layout management to the placer. place_options may include:


anchor

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)


bordermode

INSIDE (the default) to indicate that other options refer to the parent's inside (ignoring the parent's border); OUTSIDE otherwise


height width

Height and width in pixels


relheight relwidth

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


relx rely

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


x, y

Horizontal and vertical offset in pixels

place_forget

w.place_forget( )

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).

place_info

w.place_info( )

Returns a dictionary with the current place_options of w.



Previous Page
Next Page