Layout in R TclTk

There are two main commands in Tk which are used to specify the layout of widgets on a window, tkpack() and tkgrid(). tkgrid() is newer and more flexible. Here is how it can be used.

With tkgrid(), “the grid manager”, it is possible to specify absolute row and column numbers for widgets on a window, but this is not recommended, because it makes it difficult to insert new widgets later on (i.e. you would have to update all the row/column numbers). It ispossible to leave postion empty by inserting blank labels. It may be useful to sketch the grid on paper before starting coding your Tk widgets latyout.

Generally when using tkgrid() without specifying a row or column number, multiple arguments in the same call to tkgrid() are placed sequentially on the same row, and a subsequent call to tkgrid() will place widgets on the next row.

One very useful option in the tkgrid() function is the sticky = option. The value of this option can be an empty string, or any combination of the letters "n", "e", "s" and "w", e.g. "nsw". If just one letter is specified, then the widget is aligned at that edge of the grid cell (north, east, south or west). If two opposite directions are specified, e.g. "ns", then the widget is stretched from the top of the cell to the bottom. If this is impossible, then it is just centered vertically, as it would be if neither "n" nor "s" were specified. If three letters are specified, e.g. "sew", then the widget is stretched in one direction (in this case horizontally - between east and west), and aligned at the bottom (south) edge of the grid cell.

The code above produces the following window: