Thursday, January 17, 2019

Widget Placement

Geometry management is responsible for the precise position and size of a widget, for widget’s position and size relative to the other widgets, and of renegotiating these factors when conditions change, for example when the window is resized. Tkinter provides three geometry managers, therefore allowing the user to quickly and efficiently build user interfaces, with maximal control over the disposition of the widgets. The geometry managers also allow the user make abstraction of the specific implementation of the GUI on each platform that Tkinter supports, thus making the design truly portable.

A. The “Pack” geometry manager

The packer is the quickest way to design user interfaces using Tkinter. It allows the user to place the widgets relative to their contained widget. It is the most commonly used geometry manager as it allows a fair amount of flexibility. The packer positions the slave widgets on the master widget (container) from the edges to the center, each time using the space left in the master widget by previous packing operations. The options that can be used in the pack() method are:

1. expand - Specifies whether the widget should expand to fill the available space (at packing time and on window resize). It can have YES (1), NO(0) values.

2. fill - Specifies how the slave should be resized to fill the available space (if the slave is smaller than the available space). It can have NONE, X, Y, BOTH values.

3. side - Specifies which side of the master should be used for the slave. It can have TOP (default),
BOTTOM, LEFT, RIGHT values.

4. in (’in’) - Packs the slaves inside the widget passed as a parameter. This option is usually left out, in which case the widgets are packed in their parent. Restriction: widgets can only be packed inside their parents or descendants of their parent. It can have a Widget value.

5. padx,pady - Specifies the space that must be left out between two adjacent widgets. It can have Integer values .

6. ipadx, ipady - Specifies the size of the optional internal border left out when packing. It can have Integer values .

7. anchor - Specifies where the widget should be placed in the space that it has been allocated by the packer, if this space is greater than the widget size. It can have N, S, W, E,NW, SW, NE,SE, NS, EW,
NSEW, CENTER (default) values.

The “Pack” geometry manager defines the following methods for working with widgets:

pack(option=value,. . . ) , pack configure(option=value,. . . ) - Packs the widget with the specified options.
pack forget() - The widget is no longer managed by the Pack manager, but is not destroyed.
pack info() - Returns a dictionary containing the current options for the widget.
pack slaves() - Returns a list of widget IDs, in the packing order, which are slaves of the master widget.

B. The “Grid” Geometry Manager

The grid geometry manager is used for more complex layouts. It allows the user to virtually divide the master widget into several rows and columns, which can then be used to place slave widgets more precisely. The pack geometry manager would require the use of multiple nested frames to obtain the same effect.

The grid geometry manager allows the user to build a widget grid using an approach that is very similar to building tables using HTML. The user builds the table specifying not only the row and column at which to place the widget, but also the row span and column span values to use for the widget. In addition to that, the sticky option can allow almost any placement of the widget inside a cell space (if it is bigger than the widget itself). Combinations of values for the sticky option also allow to resize the widget, such as EW value, equivalent to an expand option combined with a fill=X for the packer.

The empty rows and columns are not displayed by the grid geometry manager, even if a minimum size is specified. The grid manager cannot be used in combination with the pack manager, as this results in an infinite negotiation loop.

The options that can be used in the grid() method are:

1. row, column - Specifies where the widget should be positioned in the master grid. It can have Integer values values.

2. rowspan, columnspan - Specifies the number of rows / columns the widget must span across. It can have Integer values values.

3. in (’in’) - Uses the widget passed as a parameter as the master. This option is usually left out, in which case the widgets are packed in their parent. Restriction: widgets can only be packed inside their parents or descendants of their parent. It can have a Widget value.

4. padx,pady - Specifies the space that must be left out between two adjacent widgets. It can have Integer values .

5. ipadx, ipady - Specifies the size of the optional internal border left out when packing. It can have Integer values .

6. sticky - Specifies where the widget should be placed in the space that it has been allocated by the grid manager, if this space is greater than the widget size. Default is to center the widget, but the grid() method does not support the CENTER value for sticky. It can have N, S, W, E, NW,SW, NE, SE, NS,EW, NSEW values .

The “Grid” geometry manager defines the following methods for working with widgets:

1. grid(option=value,. . . ), grid configure(option=value,. . . ) - Places the widget in a grid, using the specified options.

2. grid forget(), grid remove() - The widget is no longer managed by the Grid manager, but is not destroyed.

3. grid info() - Returns a dictionary containing the current options.

4. grid slaves() - Returns a list of widget IDs which are slaves of the master widget.

5. grid location(x,y) - Returns a (column, row) tuple which represents the cell in the grid that is closest to the point (x, y).

6. grid size() - Returns the size of the grid, in the form of a (column, row) tuple, in which column is the index of the first empty column and row the index of the first empty row.

C. The “Place” Geometry Manager

The place geometry manager is the most powerful manager of all since it allows exact placement of the widgets inside a master widget (container). However, it is more difficult to use and usually represents a great amount of overhead that is rarely needed.

The Place geometry manager allows placement of widgets using either exact coordinates (with the x and y options), or as a percentage relative to the size of the master window (expressed as a float in the range [0.0, 1.0]) with the relx and rely options. The same principle holds for the widget size (using width / height and/or relwidth / relheight). The options that can be used in the place() method are:

1. anchor - Specifies which part of the widget should be placed at the specified position. It can have N, NE, E, SE,SW, W, NW (Default),CENTER values.

2. bordermode - Specifies if the outside border should be taken into consideration when placing the widget. It can have INSIDE, OUTSIDE values.

3. in (’in’) - Places the slave in the master passed as the value for this option. It can have a Widget value.

4. relwidth, relheight - Size of the slave widget, relative to the size of the master. It can have Float [0.0, 1.0] value.

5. relx, rely - Relative position of the slave widget. It can have Float [0.0, 1.0] value.

6. width, height - Absolute width and height of the slave widget. It can have Integer values.
.
7. x, y - Absolute position of the slave widget. It can have Integer values.

The “Place” geometry manager defines the following methods for working with widgets:

1. place(option=value,. . . ), place configure(option=value,. . . ) - Places the widget with the specified options.

2. place forget() - The widget is no longer managed by the Place manager, but is not destroyed.

3. place info() - Returns a dictionary containing the current options for the widget.

4. place slaves() - Returns a list of widget IDs which are slaves of the master widget.

Here I am ending today's discussion. Till we meet next keep practicing and learning Python as Python is easy to learn!







Share:

0 comments:

Post a Comment