home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Previous Section Next Section

16.8 Geometry Management

In all the examples so far, we have made each widget visible by calling method pack on the widget. This is representative of real-life Tkinter usage. However, two other layout managers exist and are sometimes useful. This section covers all three layout managers provided by the Tkinter module.

Never mix geometry managers for the same container widget: all children of each given container widget must be handled by the same geometry manager, or very strange effects (including Tkinter going into infinite loops) may result.

16.8.1 The Packer

Calling method pack on a widget delegates widget geometry management to a simple and flexible layout manager component called the Packer. The Packer sizes and positions each widget within a container (parent) widget, according to each widget's space needs (including options padx and pady). Each widget w supplies the following Packer-related methods.

pack

w.pack(**pack_options)

Delegates geometry 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 invisible, and 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.

16.8.2 The Gridder

Calling method grid on a widget delegates widget geometry management to a specialized layout manager component 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 geometry management to the gridder. grid_options may include:

column

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

columnspan

How many columns w occupies; default 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; default the first row that is still empty.

rowspan

How many rows w occupies; default 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, compass directions indicating 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 invisible, and 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.

16.8.3 The Placer

Calling method place on a widget explicitly handles widget geometry management, thanks to a simple layout manager component 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 geometry management to the placer. place_options may include:

anchor

The exact spot of w other options refer to: may be N, E, S, W, NE, NW, SE, or SW, compass directions indicating the corners and sides of w; default is NW (the 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, 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, 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 invisible, and 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 Section Next Section