grid(3tk)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ grid(3tk) ✨

okay bestie, buckle UP, because this is the Tk Built-In Commands manual page for grid, and it's a whole vibe πŸ’…


🌸 NAME 🌸

grid β€” the geometry manager that arranges widgets in a grid. yes, like a spreadsheet but make it ✨aesthetic✨


πŸŽ€ SYNOPSIS πŸŽ€

grid option arg ?arg ...?

πŸ’– DESCRIPTION πŸ’–

so the grid command is how you talk to the grid geometry manager, and what that does is arrange your widgets in rows and columns inside of another window. that other window is called the geometry container (or just container window, same thing bestie 🫢).

the grid command has several forms depending on what you pass as the option argument. let's go, one by one, no skipping πŸ”₯

  • grid window ?window ...? ?options? β€” if the first argument to grid works as a first window argument to grid configure β€” and by that we mean either a window name (any value starting with .) or one of the characters x or ^ (peep the RELATIVE PLACEMENT section down below) β€” then this command gets processed exactly the same way as grid configure. same energy, same outcome, periodt.
  • grid anchor window ?anchor? β€” the anchor value controls how you place the grid inside the container window when literally no row/column has any weight. like, none of them are pulling their weight (literally) so where does the grid sit? that's what anchor decides. check THE GRID ALGORITHM below for the deets. the default anchor is nw. πŸ‘‘
  • grid bbox window ?column row? ?column2 row2? β€” with zero arguments, you get back the bounding box (in pixels) of the grid. the return value is 4 integers: the first two are the pixel offset from the container window (x first, then y) of the top-left corner of the grid, and the next two integers are the width and height of the grid, also in pixels. if you specify a single column and row, you get the bounding box for that specific cell instead β€” and heads up, the top left cell is numbered from zero (programmers, amirite 😭). if you specify both column and row arguments, then you get the bounding box spanning the rows and columns you indicated. multi-cell slay.
  • grid columnconfigure window index ?-option value...? β€” query or set the column properties of the index column of the geometry container, window. the valid options are -minsize, -weight, -uniform and -pad. if you provide one or more options, then index may be given as a list of column indices, and the configuration options will operate on all of them. indices can be integers, window names, or the keyword all. for all, the options apply to all columns currently occupied be content windows. for a window name, that window must be a content of this container, and the options apply to all columns currently occupied be the content. let's break the options down, bestie:
    • -minsize sets the minimum size, in screen units, that will be permitted for this column.
    • -weight (an integer value) sets the relative weight for apportioning any extra spaces among columns. a weight of zero (0) means the column will not deviate from its requested size. and 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. math is serving πŸ’…
    • -uniform, 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 more.
    • -pad 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.

if you specify only an option, with no value, you get back the current value of that option. and if you specify only the container window and index, you get all the current settings returned in a list of β€œ-option value” pairs. cute, right?

  • grid configure window ?window ...? ?options? β€” here the arguments are the names of one or more content windows, followed by pairs of arguments that say how to manage the content. and bestie, the characters -, x and ^ can be specified instead of a window name to alter the default location of a window, as described in the RELATIVE PLACEMENT section below. here are the supported options, and yes we're doing all of them:
    • -column n β€” insert the window so that it occupies the *n*th column in the grid. column numbers start with 0. if you don't supply this option, then the window gets arranged just to the right of the previous window specified on this call to grid, or column β€œ0” if it's the first window. and for each x that immediately precedes the window, the column position is incremented by one. so basically the x represents a blank column for this row in the grid. iconic little placeholder tbh.
    • -columnspan n β€” insert the window so that it occupies n columns in the grid. default is one column, unless the window name is followed by a -, in which case the columnspan is incremented once for each immediately following -. the window is wide, she contains multitudes πŸ¦‹
    • -in container β€” insert the window(s) in the container window given by container. the default is the first window's parent window.
    • -ipadx amount β€” amount specifies how much horizontal internal padding to leave on each side of the content. this space is added inside the content border. amount must be a valid screen distance, such as 2 or .5c. it defaults to 0.
    • -ipady amount β€” amount specifies how much vertical internal padding to leave on the top and bottom of the content. this space is added inside the content border. amount defaults to 0.
    • -padx amount β€” amount specifies how much horizontal external padding to leave on each side of the content, in screen units. amount may be a list of two values to specify padding for left and right separately. amount defaults to 0. this space is added outside the content border. (internal vs external padding is the whole plot twist here, don't mix them up πŸ™ˆ)
    • -pady amount β€” amount specifies how much vertical external padding to leave on the top and bottom of the content, in screen units. amount may be a list of two values to specify padding for top and bottom separately. amount defaults to 0. this space is added outside the content border.
    • -row n β€” insert the content so that it occupies the *n*th row in the grid. row numbers start with 0. if you don't supply this option, then the content is arranged on the same row as the previous content specified on this call to grid, or the next row after the highest occupied row if this is the first content.
    • -rowspan n β€” insert the content so that it occupies n rows in the grid. default is one row. and if the next grid command contains ^ characters instead of content that line up with the columns of this content, then the rowspan of this content is extended by one. tall queen energy πŸ‘‘
    • -sticky style β€” if a content's cell is larger than its requested dimensions, you can use this option to position (or stretch) the content within its cell. style is a string containing zero or more of the characters n, s, e or w. the string can optionally contain spaces or commas, but those are ignored, they're just there for ✨readability✨. each letter refers to a side (north, south, east, or west) that the content will β€œstick” to. if both n and s (or e and w) are specified, the content will be stretched to fill the entire height (or width) of its cavity. also, -sticky subsumes the combination of -anchor and -fill that is used by pack. the default is "", which causes the content to be centered in its cavity, at its requested size.

okay and one important thing: if any of the content is already managed by the geometry manager, then any unspecified options for them retain their previous values rather than receiving default values. so no, it doesn't quietly reset on you.

  • grid content window ?-option value? β€” if no options are supplied, you get back a list of all of the content in window, most recently managed first. option can be either -row or -column, which causes only the content in the row (or column) specified by value to be returned. filter era πŸ’…
  • grid forget window ?window ...? β€” removes each of the windows from grid for its container and unmaps their windows. the content will no longer be managed by the grid geometry manager. and the configuration options for that window are forgotten, so that if the window is managed once more by the grid geometry manager, the initial default settings are used. so like, she's gone gone, not just napping.
heads up bestie: if the last content window of the container becomes unmanaged, this will also send the virtual event <<NoManagedChild>> to the container; the container may choose to resize itself (or otherwise respond) to such a change. so your container can react to being suddenly childless 😭
  • grid info window β€” returns a list whose elements are the current configuration state of the content given by window, in the same option-value form that might be specified to grid configure. the first two elements of the list are β€œ-in container” where container is the window's container window. receipts, basically πŸ“‹
  • grid location window x y β€” given x and y values in screen units relative to the container 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. off-grid behavior, she said no ❌
  • grid propagate window ?boolean? β€” if boolean has a true boolean value such as 1 or on, then propagation is enabled for window, which must be a window name (see GEOMETRY PROPAGATION below). if boolean has a false boolean value then propagation is disabled for window. in either of these cases an empty string is returned. if boolean is omitted, then the command returns 0 or 1 to indicate whether propagation is currently enabled for window.
btw, propagation is enabled by default. just so you know.

  • grid rowconfigure window index ?-option value...? β€” Query or set the row properties of the index row of the geometry container, window. The valid options are -minsize, -weight, -uniform and -pad.

If one or more options are provided, then index may be given as a list 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 by content windows. For a window name, that window must be a content window of this container and the options apply to all rows currently occupied by the container window.

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.

If only an option is specified, with no value, the current value of that option is returned. If only the container window and index is specified, all the current settings are returned in a list of β€œ-option value” pairs.

  • grid remove window ?window ...? β€” Removes each of the windows from grid for its container and unmaps their windows. The content will no longer be managed by the grid geometry manager. However, the configuration options for that window are remembered, so that if the content window is managed once more by the grid geometry manager, the previous values are retained.

If the last content window of the container becomes unmanaged, this will also send the virtual event <<NoManagedChild>> to the container; the container may choose to resize itself (or otherwise respond) to such a change.

  • grid size container β€” Returns the size of the grid (in columns then rows) for container. The size is determined either by the content occupying the largest row or column, or the largest column or row with a -minsize, -weight, or -pad that is non-zero.
  • grid slaves window ?-option value? β€” Synonym for grid content window ?-option value?.

πŸ¦‹ RELATIVE PLACEMENT πŸ¦‹

The grid command contains a limited set of capabilities that permit layouts to be created without specifying the row and column information for each content. This permits content to be rearranged, added, or removed without the need to explicitly specify row and column information. When no column or row information is specified for a content, default values are chosen for -column, -row, -columnspan and -rowspan at the time the content is managed. The values are chosen based upon the current layout of the grid, the position of the content relative to other contents in the same grid command, and the presence of the characters -, x, and ^ in grid command where content names are normally expected.

  • - β€” This increases the -columnspan of the content to the left. Several -'s in a row will successively increase the number of columns spanned. A - may not follow a ^ or a x, nor may it be the first content argument to grid configure.
  • x β€” This leaves an empty column between the content on the left and the content on the right.
  • ^ β€” This extends the -rowspan of the content above the ^'s in the grid. The number of ^'s in a row must match the number of columns spanned by the content above it.

πŸ”₯ THE GRID ALGORITHM πŸ”₯

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

To compute the minimum size of a layout, the grid geometry manager first looks at all content 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 content, whichever is greater. After that the rows or columns in each uniform group adapt to each other. Then the content 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 content, 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 containers 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 container according to the anchor value. For containers 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 container must be to just exactly meet the needs of its content, and it sets the requested width and height of the container 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 command may be used to turn off propagation for one or more containers. If propagation is disabled then grid will not set the requested width and height of the container window. This may be useful if, for example, you wish for a container window to have a fixed size that you specify.

🚧 RESTRICTIONS ON CONTAINER WINDOWS 🚧

The container for each content must either be the content's parent (the default) or a descendant of the content's parent. This restriction is necessary to guarantee that the content can be placed over any part of its container that is visible without danger of the content being clipped by its parent. In addition, all content in one call to grid must have the same container.

πŸ‘‘ STACKING ORDER πŸ‘‘

If the container for a content is not its parent then you must make sure that the content is higher in the stacking order than the container. Otherwise the container will obscure the content and it will appear as if the content has not been managed correctly. The easiest way to make sure the content is higher than the container is to create the container window first: the most recently created window will be highest in the stacking order.

πŸ’– CREDITS πŸ’–

The grid command is based on ideas taken from the GridBag geometry manager written by Doug. Stein, and the blt_table geometry manager, written by George Howlett.

✨ EXAMPLES ✨

A toplevel window containing a text widget and two scrollbars:

# Make the widgets
toplevel .t
text .t.txt -wrap none -xscroll {.t.h set} -yscroll {.t.v set}
scrollbar .t.v -orient vertical   -command {.t.txt yview}
scrollbar .t.h -orient horizontal -command {.t.txt xview}

# Lay them out
grid .t.txt .t.v -sticky nsew
grid .t.h        -sticky nsew

# Tell the text widget to take all the extra room
grid rowconfigure    .t .t.txt -weight 1
grid columnconfigure .t .t.txt -weight 1

Three widgets of equal width, despite their different β€œnatural” widths:

button .b -text "Foo"
entry .e -textvariable foo ; set foo "Hello World!"
label .l -text "This is a fairly long piece of text"

grid .b .e .l -sticky ew
grid columnconfigure . "all" -uniform allTheSame

🫢 SEE ALSO 🫢

pack(3tk), place(3tk)

πŸ“ KEYWORDS πŸ“

geometry manager, location, grid, cell, propagation, size, pack


That's the tea, bestie! This manual page is from Tk 8.5, grid(3tk). Stay slay. πŸ’…

πŸ“¦ where this page comes from