grid() - Geometry manager that arranges widgets in a grid

SYNOPSIS

instance.grid([**options])

DESCRIPTION

The grid() method is used to communicate with the grid geometry manager that arranges widgets in rows and columns inside of another window, called the geometry parent (or parent window). The grid() method can have any of several forms:

child.grid([**options])
The grid() command is processed in the same way as grid_configure().

parent.grid_ columnconfigure(index [,**options])
parent.columnconfigure(index [,**options])
Query or set the column properties of the index column of the geometry parent, parent. The valid options are minsize, weight, uniform and pad. The index may be given as a list (or tuple) of column indices to which the configuration options will operate on. Indices may be integers, window names or the keyword "all". For "all" the options apply to all columns currently occupied be child windows. For a window name, that window must be a child of this parent and the options apply to all columns currently occupied be the child.
The minsize option sets the minimum size, in screen units, that will be permitted for this column.
The weight option (an integer value) sets the relative weight for apportioning any extra spaces among columns. A weight of zero (0) indicates the column will not deviate from its requested size. A column whose weight is two will grow at twice the rate as a column of weight one when extra space is allocated to the layout.
The uniform option, when a non-empty value is supplied, places the column in a uniform group with other columns that have the same value for uniform. The space for columns belonging to a uniform group is allocated so that their sizes are always in strict proportion to their weight values. See THE GRID ALGORITHM below for further details.
The pad option specifies the number of screen units that will be added to the largest window contained completely in that column when the grid geometry manager requests a size from the containing window.

child.grid_configure([**options])
The options specify how to manage the slaves. The following options are supported:
  • column=n
    Insert the child so that it occupies the nth column in the grid. Column numbers start with 0. If this option is not supplied, then the child is arranged just to the bellow of previous child, or column “0” if it is the first child.

  • ipadx=amount
    The amount specifies how much horizontal internal padding to leave on each side of the child. This is space is added inside the child border. The amount must be a valid screen distance, such as 2 or ".5c". It defaults to 0.

  • ipady=amount
    The amount specifies how much vertical internal padding to leave on the top and bottom of the child. This space is added inside the child border. The amount defaults to 0.

  • padx=amount
    padx=(value1 ,value2)
    padx=" value1 value2"
    The amount specifies how much horizontal external padding to leave on each side of the child, in screen units. Amount may be a list of two values to specify padding for left and right separately. The amount defaults to 0. This space is added outside the slave(s) border.

  • pady=amount
    pady=(value1 ,value2)
    pady=" value1 value2"
    The amount specifies how much vertical external padding to leave on the top and bottom of the child, in screen units. Amount may be a list of two values to specify padding for top and bottom separately. The amount defaults to 0. This space is added outside the slave(s) border.

  • row=n
    Insert the child so that it occupies the nth row in the grid. Row numbers start with 0. If this option is not supplied, then the child is arranged on the same row as the previous child, or the first unoccupied row if this is the first child.

  • sticky="style"
    If a child's cell is larger than its requested dimensions, this option may be used to position (or stretch) the child within its cell. "style" is a string that contains zero or more of the characters "n", "s", "e" or "w". The string can optionally contains spaces or commas, but they are ignored. Each letter refers to a side (north, south, east, or west) that the child will “stick” to. If both "n" and "s" (or "e" and "w") are specified, the child will be stretched to fill the entire height (or width) of its cavity. The sticky option subsumes the combination of anchor and fill that is used by pack(). The default is “”, which causes the child to be centered in its cavity, at its requested size.
If any of the childs are already managed by the geometry manager then any unspecified options for them retain their previous values rather than receiving default values.

child.grid_forget()
Removes the child from grid for its parent and unmaps its window. The child will no longer be managed by the grid geometry manager. The configuration options for that window are forgotten, so that if the child is managed once more by the grid geometry manager, the initial default settings are used.

child.grid_info()
Returns a list whose elements are the current configuration state of the child given by child in the form:
{'in': parent, 'column': n, 'row': n, 'columnspan': n, 'rowspan': n, 'ipadx': amount, 'ipady': amount, 'padx': amount, 'pady': amount, 'sticky': 'style'}

parent.grid_location(x ,y)
Given x and y values in screen units relative to the parent window, the column and row number at that x and y location is returned. For locations that are above or to the left of the grid, -1 is returned.

parent.grid_propagate([boolean])
If boolean has a true boolean value such as 1 or True then propagation is enabled for parent (see GEOMETRY PROPAGATION below). If boolean has a false boolean value then propagation is disabled for parent. In either of these cases None is returned. If boolean is omitted then the command returns False or True to indicate whether propagation is currently enabled for parent. Propagation is enabled by default.

master.grid_rowconfigure(index [,**options])
master.rowconfigure(index [,**options])
Query or set the row properties of the index row of the geometry parent, parent. The valid options are minsize, weight, uniform and pad. The index may be given as a list (or tuple) of row indices to which the configuration options will operate on. Indices may be integers, window names or the keyword "all". For "all" the options apply to all rows currently occupied be child windows. For a window name, that window must be a child of this parent and the options apply to all rows currently occupied be the child.
The minsize option sets the minimum size, in screen units, that will be permitted for this row.
The weight option (an integer value) sets the relative weight for apportioning any extra spaces among rows. A weight of zero (0) indicates the row will not deviate from its requested size. A row whose weight is two will grow at twice the rate as a row of weight one when extra space is allocated to the layout.
The uniform option, when a non-empty value is supplied, places the row in a uniform group with other rows that have the same value for uniform. The space for rows belonging to a uniform group is allocated so that their sizes are always in strict proportion to their weight values. See THE GRID ALGORITHM below for further details.
The pad option specifies the number of screen units that will be added to the largest window contained completely in that row when the grid geometry manager requests a size from the containing window.

child.grid_remove()
Removes the child from grid for its parent and unmaps its window. The child will no longer be managed by the grid geometry manager. However, the configuration options for that window are remembered, so that if the child is managed once more by the grid geometry manager, the previous values are retained.

parent.grid_size()
Returns the size of the grid (in columns then rows) for parent.

parent.grid_slaves([column [,row]])
If no options are supplied, a list of all of the childs in parent are returned, most recently manages first.

THE GRID ALGORITHM

The grid geometry manager lays out its childs in three steps. In the first step, the minimum size needed to fit all of the childs is computed, then (if propagation is turned on), a request is made of the master window to become that size. In the second step, the requested size is compared against the actual size of the master. If the sizes are different, then spaces is added to or taken away from the layout as needed. For the final step, each child is positioned in its row(s) and column(s) based on the setting of its sticky option.

To compute the minimum size of a layout, the grid geometry manager first looks at all childs whose columnspan and rowspan values are one, and computes the nominal size of each row or column to be either the minsize for that row or column, or the sum of the padding plus the size of the largest child, whichever is greater. After that the rows or columns in each uniform group adapt to each other. Then the childs whose row-spans or column-spans are greater than one are examined. If a group of rows or columns need to be increased in size in order to accommodate these childs, then extra space is added to each row or column in the group according to its weight. For each group whose weights are all zero, the additional space is apportioned equally.

When multiple rows or columns belong to a uniform group, the space allocated to them is always in proportion to their weights. (A weight of zero is considered to be 1.) In other words, a row or column configured with weight=1 ,uniform="a" will have exactly the same size as any other row or column configured with weight=1 ,uniform="a". A row or column configured with weight=2 ,uniform="b" will be exactly twice as large as one that is configured with weight=1 ,uniform="b".
More technically, each row or column in the group will have a size equal to k*weight for some constant k. The constant k is chosen so that no row or column becomes smaller than its minimum size. For example, if all rows or columns in a group have the same weight, then each row or column will have the same size as the largest row or column in the group.

For parents whose size is larger than the requested layout, the additional space is apportioned according to the row and column weights. If all of the weights are zero, the layout is placed within its parent according to the anchor value. For parents whose size is smaller than the requested layout, space is taken away from columns and rows according to their weights. However, once a column or row shrinks to its minsize, its weight is taken to be zero. If more space needs to be removed from a layout than would be permitted, as when all the rows or columns are at their minimum sizes, the layout is placed and clipped according to the anchor value.

GEOMETRY PROPAGATION

The grid geometry manager normally computes how large a parent must be to just exactly meet the needs of its childs, and it sets the requested width and height of the parent to these dimensions. This causes geometry information to propagate up through a window hierarchy to a top-level window so that the entire sub-tree sizes itself to fit the needs of the leaf windows. However, the grid_propagate() method may be used to turn off propagation for one or more parents. If propagation is disabled then grid will not set the requested width and height of the parent window. This may be useful if, for example, you wish for a parent window to have a fixed size that you specify.

Comments