## 4.3 The Grid Geometry Manager

The grid geometry arranges widgets in a grid-like fashion. For each widget to be managed by the `grid` command, a row and a column number is given. The manager computes parcels for the widgets such that all parcels in the same column have the same width and all parcels in the same row have the same height.

Figure 4.7 shows how eight labels are placed by the `grid` command. Note that it is not necessary that all positions in the grid are occupied by a widget. In our example in Figure 4.7, the position at row and column `2` does not contain a widget.

  `proc {GL W R C S}
  L={New Tk.label tkInit(parent:W text:S)}
  in
  {Tk.send grid(L row:R column:C padx:4 pady:4)}
  end
  {GL W 1 1 nw} {GL W 1 2 north} {GL W 1 3 ne}
  {GL W 2 1 west} {GL W 2 3 east}
  {GL W 3 1 sw} {GL W 3 2 south} {GL W 3 3 sw}`

Figure 4.7: Using the `grid` command.

### 4.3.1 Padding

The `grid` command supports padding in the same way as the packer does. In the above example we used external padding by giving `padx` and `pady` options. It is also possible to use internal padding with the options `ipadx` and `ipady`.

### 4.3.2 Span Options

The grid command can also compute geometries where widgets occupy more than a single row or column. In the example shown in Figure 4.8 the label widget `L` is managed by the `grid` command to occupy both two rows and two columns. How much rows and columns a widget's parcel spans is specified with the `columnspan` and `rowspan` options.

  `{Tk.send grid({New Tk.label tkInit(parent:W text:'Upper left')}
  row:1 rowspan:2
  column:1 columnspan:2
  padx:4 pady:4)}
  {GL W 1 3 ne} {GL W 2 3 east}
  {GL W 3 1 sw} {GL W 3 2 south} {GL W 3 3 sw}`

Figure 4.8: Using the `columnspan` and `rowspan` options.

### 4.3.3 Sticky Options

The `grid` command combines the `anchor` and `fill` options from the packer in a single `sticky` option. The value given for a sticky option determines both the side the widget is placed in its parcel, and how the widget is to be stretched to fill its parcel.

Valid values for the `sticky` option are all combinations of the letters `n`, `s`, `w`, and `e` in any order. Giving one of `n` and `s` (or of `w` and `e`) specifies the anchor position of a widget. Giving both `n` and `s` (or both `w` and `e`) requests that the widget should fill its parcel horizontally (or vertically). For an example see Figure 4.9.

  `{Tk.send grid({New Tk.label tkInit(parent:W text:'Upper left')}
  row:1 rowspan:2
  column:1 columnspan:2
  sticky: nse
  padx:4 pady:4)}`

Figure 4.9: Using the `sticky` option with the `grid` command.

### 4.3.4 Weight Options

The grid geometry manager employs a different scheme for expansion of parcels than the packer. Rows and columns in the grid can be assigned an integer weight. Additional space available in the master of the grid is distributed between the rows and columns according to their relative weight.

For example, if we take the last example and want that all additional space is given to the third row and third column, we can do this by

  `{Tk.batch [grid(rowconfigure W 3 weight:1)
  grid(columnconfigure W 3 weight:1)]}`

Figure 4.10 shows the result of resizing the window.

Figure 4.10: Result of resizing a window.