Python in a Nutshell, Second Edition (In a Nutshell)
| 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.
Delegates geometry management to the packer. pack_options may include:
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).
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.
Delegates geometry management to the gridder. grid_options may include:
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.
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).
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.
Delegates geometry management to the placer. place_options may include:
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).
Returns a dictionary with the current place_options of w. |