There are two commands in Tk which are used to specify the layout of widgets on a window, tkpack
and tkgrid. tkgrid is newer and I have found that I prefer to use this exclusively.
The first version of this Layout in R TclTk webpage will only talk about tkgrid. Later I will add examples
using tkpack. Peter Dalgaard discusses both layout managers in his R TclTk Primer in
Rnews 2001, Vol. 3.
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). I quite like having some blank
lines and/or columns in my windows, and I generally achieve this by inserting blank text labels in the grid,
i.e. labels containing only spaces. It may be useful for you to sketch your grid on paper in order to check
that your R code is correct or to design your code before you write it :) .
Generally when you use 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 reader should refer back to the scrollbar examples which use the sticky option to stretch them to the appropriate length and align them next to the appropriate list box or text widget.
require(tcltk) tt <- tktoplevel() tkgrid(tklabel(tt,text="Here is a centered string of text.")) tkgrid(tklabel(tt,text="Left"),sticky="w") tkgrid(tklabel(tt,text="Right"),sticky="e") tkgrid(tklabel(tt,text=" ")) # Blank line tkgrid(tklabel(tt,text=" ")) # Blank line tkgrid(tklabel(tt,text="Here is a much longer string of text, which takes up two columns."),columnspan=2) LeftLabel <- tklabel(tt,text="Left") # Here, Left and Right labels RightLabel <- tklabel(tt,text="Right") # are in separate rows. tkgrid(LeftLabel,RightLabel) tkgrid.configure(LeftLabel,sticky="w") tkgrid.configure(RightLabel,sticky="e") LeftLabel2 <- tklabel(tt,text="LeftAligned") RightLabel2 <- tklabel(tt,text="RightAligned") tkgrid(RightLabel2,LeftLabel2) # Here, Left and Right labels are in the same row tkgrid.configure(RightLabel2,sticky="e") tkgrid.configure(LeftLabel2,sticky="w") tkgrid(tklabel(tt,text=" ")) # Blank line tkgrid(tklabel(tt,text=" ")) # Blank line tkgrid(tklabel(tt,text="This sentence takes up two rows,\n but only one column"),rowspan=2) tkfocus(tt)
The code above produces the following window:
