grid(3pm)
β¨ NAME β¨
Tk::grid β geometry manager that arranges widgets in a grid. This is the grid(3pm) manual page, bestie, and we're about to make grid layout make sense. π
π SYNOPSIS π
$widget->grid?(?widget ...,? ?arg ?...>?)?
$widget->gridOption?(arg ?,arg ...?)?
πΈ DESCRIPTION πΈ
Okay so the grid method is how you talk to the grid geometry manager. It arranges widgets in rows and columns inside another window, called the geometry master (or master window). The grid method can have any of several forms, depending on the option argument:
π
$slave->grid(?$slave, ...??, options?)
The arguments are the optional references to more slave windows, followed by pairs of arguments that specify how to manage the slaves. The characters -, x and ^ can be specified instead of a window reference to alter the default location of a $slave, as described in "RELATIVE PLACEMENT", below.
If any of the slaves are already managed by the geometry manager, then any unspecified options for them retain their previous values rather than receiving default values.
The following options are supported:
-column => nβ Insert the$slaveso that it occupies the nth column in the grid. Column numbers start with0. If this option is not supplied, then the$slaveis arranged just to the right of previous slave specified on this call to grid, or column"0"if it is the first slave. For eachxthat immediately precedes the$slave, the column position is incremented by one. Thus thexrepresents a blank column for this row in the grid. β¨-columnspan => nβ Insert the slave so that it occupies n columns in the grid. The default is one column, unless the window name is followed by a-, in which case the columnspan is incremented once for each immediately following-.-in => $otherβ Insert the slave(s) in the master window given by$other. The default is the first slave's parent window.-ipadx => amountβ Theamountspecifies how much horizontal internal padding to leave on each side of the slave(s). This space is added inside the slave(s) border. Theamountmust be a valid screen distance, such as2or'.5c'. It defaults to0.-ipady => amountβ Theamountspecifies how much vertical internal padding to leave on the top and bottom of the slave(s). This space is added inside the slave(s) border. Theamountdefaults to0.-padx => amountβ Theamountspecifies how much horizontal external padding to leave on each side of the slave(s), in screen units. Theamountdefaults to0. This space is added outside the slave(s) border.-pady => amountβ Theamountspecifies how much vertical external padding to leave on the top and bottom of the slave(s), in screen units. Theamountdefaults to0. This space is added outside the slave(s) border.-row => nβ Insert the slave so that it occupies the nth row in the grid. Row numbers start with0. If this option is not supplied, then the slave is arranged on the same row as the previous slave specified on this call to grid, or the first unoccupied row if this is the first slave.-rowspan => nβ Insert the slave so that it occupies n rows in the grid. The default is one row. If the next grid method contains^characters instead of$slavesthat line up with the columns of this$slave, then the rowspan of this$slaveis extended by one.-sticky => styleβ If a slave's cell is larger than its requested dimensions, this option may be used to position (or stretch) the slave within its cell.Styleis a string that contains zero or more of the charactersn,s,eorw. The string can optionally contain spaces or commas, but they are ignored. Each letter refers to a side (north, south, east, or west) that the slave will "stick" to. If bothnands(oreandw) are specified, the slave will be stretched to fill the entire height (or width) of its cavity. The sticky option subsumes the combination of-anchorand-fillthat is used bypack. The default is'', which causes the slave to be centered in its cavity, at its requested size.
π $master->gridBbox(?column, row,? ?column2, row2?)
With no arguments, the bounding box (in pixels) of the grid is returned. The return value consists of 4 integers. The first two are the pixel offset from the master window (x then y) of the top-left corner of the grid, and the second two integers are the width and height of the grid, also in pixels. If a single column and row is specified on the command line, then the bounding box for that cell is returned, where the top left cell is numbered from zero. If both column and row arguments are specified, then the bounding box spanning the rows and columns indicated is returned.
π $master->gridColumnconfigure(index?, -option=>value, ...?)
Query or set the column properties of the index column of the geometry master, $master. The valid options are -minsize, -weight and -pad. If one or more options are provided, then index may be given as a list of column indices to which the configuration options will operate on. 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. If only an option is specified, with no value, the current value of that option is returned. If only the master window and index is specified, all the current settings are returned in an list of "-option value" pairs.
π¦ $slave->gridConfigure(?$slave, ...?, options?)
The same as grid method.
π $slave->gridForget?($slave, ...)?
Removes each of the $slaves from grid for its master and unmaps their windows. The slaves will no longer be managed by the grid geometry manager. The configuration options for that window are forgotten, so that if the slave is managed once more by the grid geometry manager, the initial default settings are used.
π $slave->gridInfo
Returns a list whose elements are the current configuration state of the slave given by $slave in the same option-value form that might be specified to gridConfigure. The first two elements of the list are -in=>$master where $master is the slave's master.
π $master->gridLocation(x, y)
Given x and y values in screen units relative to the master 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.
β¨ $master->gridPropagate?(boolean)?
If boolean has a true boolean value such as 1 or on then propagation is enabled for $master, which must be a window name (see "GEOMETRY PROPAGATION" below). If boolean has a false boolean value then propagation is disabled for $master. In either of these cases an empty string is returned. If boolean is omitted then the method returns 0 or 1 to indicate whether propagation is currently enabled for $master. Propagation is enabled by default.
π $master->gridRowconfigure(index?, -option=>value, ...?)
Query or set the row properties of the index row of the geometry master, $master. The valid options are -minsize, -weight and -pad. If one or more options are provided, then index may be given as a list of row indeces to which the configuration options will operate on. 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 master window and index is specified, all the current settings are returned in an list of "option-value" pairs.
π₯ $slave->gridRemove?($slave, ...)?
Removes each of the $slaves from grid for its master and unmaps their windows. The slaves will no longer be managed by the grid geometry manager. However, the configuration options for that window are remembered, so that if the slave is managed once more by the grid geometry manager, the previous values are retained.
π $master->gridSize
Returns the size of the grid (in columns then rows) for $master. The size is determined either by the $slave occupying the largest row or column, or the largest column or row with a -minsize, -weight, or -pad that is non-zero.
$master->gridSlaves?(-option=>value)?
Bestie, if you hand over zero options, you get back a list of every single slave chillin' in $master, sorted most recently managed first. And if you do pass an option? It's gotta be either -row or -column, and then you only get the slaves sitting in the row (or column) you named in value. That's it, that's the whole vibe. β¨
π¦ RELATIVE PLACEMENT π¦
So the grid method comes with a limited little set of abilities that let you build layouts without spelling out the row and column info for every single slave. Which is iconic, because it means you can rearrange, add, or yeet slaves and never have to go back and hand-write row/column data. π«Ά
When you don't specify column or row info for a $slave, the defaults for -column, -row, -columnspan and -rowspan get picked at the moment the $slave is managed. And what gets picked depends on:
- the current layout of the grid,
- where the
$slavesits relative to the other$slavesin the same grid method, - and whether the characters
-,^, and^show up in the grid method where$slavenames would normally go. π
Here's the cheat sheet, no cap:
-β this bumps up the columnspan of the$slaveto the left. Stack a few-'s in a row and the columnspan keeps growing, one after another. And listen: a-may not follow a^or ax. π«xβ this leaves an empty column sitting between the$slaveon the left and the$slaveon the right. A lil breathing room, periodt. π^β this extends the-rowspanof the$slaveabove the^'s in the grid. And the number of^'s in a row has to match the number of columns spanned by the$slaveabove it. Matchy matchy or it doesn't work. π
π₯ THE GRID ALGORITHM π₯
Okay so the grid geometry manager lays out its slaves in three steps, and I need you to lock in because this is the main character energy of the whole page:
- First, it figures out the minimum size needed to fit all the slaves. Then β if propagation is turned on β it asks the master window to become exactly that size.
- Second, it takes the requested size and compares it against the actual size of the master. If they don't match, space gets added to or taken away from the layout as needed.
- Final step: every slave gets positioned in its row(s) and column(s), based on whatever its
stickyflag is set to. π«‘
Now, to compute the minimum size of a layout, the grid geometry manager does this:
First it looks at all slaves whose columnspan and rowspan are both 1, and it computes the nominal size of each row or column as whichever is greater β either the minsize for that row or column, or the sum of the padding plus the size of the largest slave. Then it goes and examines the slaves whose rowspans or columnspans are greater than 1. If some group of rows or columns needs to grow so those slaves can fit, extra space gets added to each row or column in that group according to its weight. And for any group where all the weights are zero, that extra space gets split up equally. π
When multiple rows or columns belong to a uniform group, the space handed to them is always in proportion to their weights. (A weight of zero counts as 1, btw.) In other words: a row or column configured with -weight 1 -uniform a will be exactly the same size as any other row or column configured with -weight 1 -uniform a. And a row or column configured with -weight 2 -uniform b will be exactly twice as large as one configured with -weight 1 -uniform b. Math is serving. π
Getting more technical for a sec β each row or column in the group gets a size equal to k*weight for some constant k. And k is chosen so that no row or column ends up smaller than its minimum size. For example: if every row or column in a group has the same weight, then each one will be the same size as the largest row or column in that group. π¦
For masters whose size is larger than the requested layout, the extra space gets apportioned according to the row and column weights. But if all the weights are zero, the layout just gets centered inside its master. And for masters whose size is smaller than the requested layout, space gets taken away from columns and rows according to their weights. Here's the catch though: once a column or row shrinks down to its minsize, its weight is treated as zero. And if more space needs to be removed than is actually allowed β like when all the rows or columns are already at their minimum sizes β then the layout gets clipped on the bottom and right. π
π§ GEOMETRY PROPAGATION π§
The grid geometry manager normally computes how big a master has to be to just exactly meet the needs of its slaves, and then it sets the requested width and height of the master to those dimensions. Which means geometry info propagates up through the window hierarchy to a top-level window, so the whole sub-tree sizes itself to fit the needs of the leaf windows. Cute, right? πΈ
But β and this is the plot twist β the gridPropagate method can be used to turn off propagation for one or more masters. If propagation is disabled, grid won't set the requested width and height of the master window. So if you want a master window to have a fixed size that you specify? That's your moment. β¨
π RESTRICTIONS ON MASTER WINDOWS π
The master for each slave has to either be the slave's parent (that's the default) or a descendant of the slave's parent. This restriction isn't just being difficult β it's necessary to guarantee that the slave can be placed over any part of its master that's visible, without any danger of the slave getting clipped by its parent. π
Also, all slaves in one call to grid must have the same master. No mixing. Periodt.
π STACKING ORDER π
If the master for a slave isn't its parent, then you must make sure the slave is higher in the stacking order than the master. Otherwise the master will obscure the slave and it'll look like the slave hasn't been managed correctly at all β which is a whole mess you don't want. π
The easiest way to make sure the slave is higher than the master? Create the master window first. Because the most recently created window will be highest in the stacking order. Simple as that, bestie. π«Ά
β CREDITS β
The grid method is based on ideas taken from the GridBag geometry manager written by Doug. Stein, and the blt_table geometry manager, written by George Howlett. Giving credit where it's due, always. π
π SEE ALSO π
Tk::formTk::packTk::place
πΈ KEYWORDS πΈ
geometry manager, location, grid, cell, propagation, size, pack, master, slave
And that's the whole page, babe! π This one's from perl v5.40.1, dated 2025-04-13, and it lives in the manual as grid(3pm). Go forth and lay out your windows, you absolute legend. π
β¨
π¦ where this page comes from
- debian trixie: perl-tk 1:804.036+dfsg1-5
- ubuntu questing: perl-tk 1:804.036+dfsg1-5
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms