beetlebug▾
/

grid

Puts widgets in the cells of a table of rows and columns in a container.

grid WINDOW ?WINDOW ...? ?OPTION VALUE ...?
grid SUBCOMMAND ?ARG ...?

Description

The geometry manager grid puts each widget in a cell of a table in its container, or in a block of cells with -columnspan and -rowspan. A column is as wide as its widest widget, and a row is as tall as its tallest widget. grid columnconfigure and grid rowconfigure give a column or a row a minimum size, a padding, a weight for extra space, and a group of equal sizes.

A first word that starts with a dot is the same as grid configure. The words of one call are one row: each WINDOW goes in the next column. Three words in place of a WINDOW change the layout of the row.

By default the container changes its requested size to the size of the table. grid propagate turns this off. A toplevel cannot be in a grid. A widget has one geometry manager at a time. A container with widgets of pack cannot also have widgets of grid.

Subcommands

SubcommandDescription
grid configurePuts the widgets in the grid with the options, or changes the options of widgets in the grid.
grid forgetTakes widgets out of the grid and off the screen, and drops their options.
grid removeTakes widgets out of the grid and off the screen, and keeps their options.
grid infoGives the grid options of a widget.
grid slavesGives the widgets in the grid of a container, the last one first.
grid contentGives the widgets in the grid of a container, the last one first.
grid sizeGives the count of columns and of rows of the grid of a container.
grid bboxGives the box of the grid, of a cell, or of a block of cells, in pixels.
grid locationGives the column and the row of the cell at a point of a container.
grid anchorGives or sets the place of the grid in a container larger than the grid.
grid propagateGives or sets whether a container gets the size of its grid.
grid columnconfigureGives or sets the options of one or more columns of a grid.
grid rowconfigureGives or sets the options of one or more rows of a grid.

Options

OptionTypeDefaultDescription
-columnintegerthe next columnThe column of the widget, from 0.
-columnspaninteger1The count of columns that the widget fills, from its -column to the right.
-inwindow namethe parentThe container of the widgets, in place of their parent.
-ipadxscreen distance0Space added inside the widget at the left and the right.
-ipadyscreen distance0Space added inside the widget above and below.
-padxone or two screen distances0Free space outside the widget at the left and the right of its cell.
-padyone or two screen distances0Free space outside the widget above and below it in its cell.
-rowintegerthe next rowThe row of the widget, from 0.
-rowspaninteger1The count of rows that the widget fills, from its -row down.
-stickysidesemptyThe sides of its cell that the widget sticks to.

-column N

The column of the widget, from 0.

With no -column, the first WINDOW of a call goes in column 0 and each other WINDOW in the column after the last one.

% frame .co
> frame .co.a
> grid .co.a -column 3
> dict get [grid info .co.a] -column
3
% frame .co2
> grid .co2 -column -1
bad column value "-1": must be a non-negative integer
-errorcode NONE

-columnspan N

The count of columns that the widget fills, from its -column to the right.

The widget gets the width of N columns and their padding. A - after the widget in the call adds one column.

% frame .cs
> frame .cs.a
> grid .cs.a -columnspan 2
> grid size .cs
2 1
% frame .cs2
> grid .cs2 -columnspan 0
bad columnspan value "0": must be a positive integer
-errorcode NONE

-in CONTAINER

The container of the widgets, in place of their parent.

CONTAINER must be the parent of the widget or a descendant of that parent, and it cannot be the widget itself.

% frame .gi1
> frame .gi2
> grid .gi2 -in .gi1
> grid slaves .gi1
.gi2
% frame .gi3
> grid .gi3 -in .gi3
window can't be managed in itself
-errorcode NONE

% frame .gi4
> frame .gi5
> button .gi5.b
> grid .gi5.b -in .gi4
can't put .gi5.b inside .gi4
-errorcode NONE

-ipadx DISTANCE

Space added inside the widget at the left and the right.

The widget gets a width of its requested width plus two times DISTANCE. The value must not be negative.

% frame .ix
> frame .ix.a -width 10 -height 10
> grid .ix.a -ipadx 5
> pack .ix
> update
> winfo width .ix.a
20
% frame .ix2
> grid .ix2 -ipadx -1
bad ipadx value "-1": must be positive screen distance
-errorcode NONE

-ipady DISTANCE

Space added inside the widget above and below.

The widget gets a height of its requested height plus two times DISTANCE. The value must not be negative.

% frame .iy
> frame .iy.a -width 10 -height 10
> grid .iy.a -ipady 3
> pack .iy
> update
> winfo height .iy.a
16
% frame .iy2
> grid .iy2 -ipady x
bad ipady value "x": must be positive screen distance
-errorcode NONE

-padx PAD

Free space outside the widget at the left and the right of its cell.

One distance puts the same space at the two sides. A list of two distances puts the first at the left and the second at the right. The space is part of the cell and stays empty. The values must not be negative.

% frame .px
> frame .px.a -width 10 -height 10
> grid .px.a -padx {3 4}
> pack .px
> update
> list [winfo reqwidth .px] [winfo x .px.a] [dict get [grid info .px.a] -padx]
17 3 {3 4}
% frame .px2
> grid .px2 -padx {1 2 3}
wrong number of parts to pad specification
-errorcode NONE

-pady PAD

Free space outside the widget above and below it in its cell.

One distance puts the same space above and below. A list of two distances puts the first above and the second below. The values must not be negative.

% frame .py
> frame .py.a -width 10 -height 10
> grid .py.a -pady 5
> pack .py
> update
> list [winfo reqheight .py] [winfo y .py.a]
20 5
% frame .py2
> grid .py2 -pady -2
bad pad value "-2": must be positive screen distance
-errorcode NONE

-row N

The row of the widget, from 0.

With no -row, a call puts its widgets in the row after the last row of the table.

% frame .ro
> frame .ro.a
> frame .ro.b
> grid .ro.a -row 2
> grid .ro.b
> list [dict get [grid info .ro.a] -row] [dict get [grid info .ro.b] -row]
2 3
% frame .ro2
> grid .ro2 -row x
bad row value "x": must be a non-negative integer
-errorcode NONE

-rowspan N

The count of rows that the widget fills, from its -row down.

The widget gets the height of N rows and their padding. A ^ in the next call, in the same column, adds one row.

% frame .rs
> frame .rs.a
> grid .rs.a -rowspan 3
> grid size .rs
1 3
% frame .rs2
> grid .rs2 -rowspan 0
bad rowspan value "0": must be a positive integer
-errorcode NONE

-sticky SIDES

The sides of its cell that the widget sticks to.

SIDES has any of the letters n, s, e, and w, in any order, with spaces or commas between them. A widget that sticks to two opposite sides stretches to fill the cell in that direction. A widget that sticks to one side of a pair moves to that side. With no letter of a pair, the widget is at the center of the cell in that direction. grid info gives the letters in the order n, e, s, w.

% frame .st -width 100 -height 40
> grid propagate .st 0
> pack .st
> frame .st.a -width 10 -height 10
> grid .st.a -sticky ew
> grid columnconfigure .st 0 -weight 1
> update
> list [winfo x .st.a] [winfo width .st.a]
0 100

% frame .st2
> frame .st2.a
> grid .st2.a -sticky {s, n}
> dict get [grid info .st2.a] -sticky
ns
% frame .st3
> grid .st3 -sticky q
bad stickyness value "q": must be a string containing n, e, s, and/or w
-errorcode NONE

Examples

# Each call is one row, and each widget of a call is in the next column.
% frame .tb
> foreach n {a b c d} { frame .tb.$n -width 20 -height 10 }
> grid .tb.a .tb.b
> grid .tb.c .tb.d
> pack .tb
> update
> list [grid size .tb] [winfo x .tb.b] [winfo y .tb.c]
{2 2} 20 10

# `-` extends the first widget over three columns, and `x` leaves the middle column of the second row empty.
% frame .rel
> frame .rel.a
> frame .rel.b
> frame .rel.c
> grid .rel.a - -
> grid .rel.b x .rel.c
> list [dict get [grid info .rel.a] -columnspan] [dict get [grid info .rel.c] -column]
3 2

% frame .up
> frame .up.a
> frame .up.b
> frame .up.c
> grid .up.a .up.b
> grid .up.c ^
> dict get [grid info .up.b] -rowspan
2

Errors

% grid
wrong # args: should be "grid option arg ?arg ...?"
-errorcode TCL WRONGARGS

% grid bogus .x
bad option "bogus": must be anchor, bbox, columnconfigure, configure, content, forget, info, location, propagate, remove, rowconfigure, size, or slaves
-errorcode NONE

% grid c .x
ambiguous option "c": must be anchor, bbox, columnconfigure, configure, content, forget, info, location, propagate, remove, rowconfigure, size, or slaves
-errorcode NONE

% grid .
can't manage ".": it's a top-level window
-errorcode NONE

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also