A simple Tk toplevel window with an OK button

The primary goal of the tcltk R package is to use the Tk graphical user interface (GUI) toolkit with R. Here is a Tk window with an OK button that just destroys the window when it is clicked:

You should get the following window:

Click OK to close the window

Note that a Tk widget is not placed automatically inside its container1. You have to use one of the three Tk geometry managers (grid, pack or place, using respectively the tcltk functions tkgrid(), tkpack() and tkplace() in R)2. The grid manager is the most powerful and the most used one. It devides the container into a grid of rows and columns, arranges nicely the widgets in the grid, and then automatically resizes the container to best match its content (resulting here in a shrinked small window around the OK button).

Our Tk window and the way we manage it is indeed far from optimal. It can be ameliorated in four ways:

1. It would be nice to give a title to our Tk window. This can be done using tktitle().

2. We could use ttk instead of tk widgets by replacing tkbutton() by ttkbutton(). The ttk widgets are styled according to a theme that makes your GUI look better, more modern, and sometimes more native (on Windows, for instance).

3. We should think about the size of the widgets and margins around them for a better layout. Our tiny OK button in the middle of a small window is not that nice. So, let’s improve this.

4. It is nice to keep track of our Tk windows and widgets by assigning variables, like win1 or butOK here. However, these variables clutter our workspace. They also do not reflect the hierarchy. butOK is embedded in win1 at the Tk level. It makes clean up more difficult once the window is destroyed: you must get rid of both win1 and butOK to free memory from items that are not needed any more. Finally, if you have two windows, each with an OK button, you should of course not call them both butOK. Also, reassigning win1 before the first window is destroyed leads to problems3. With a more complex GUI, you easily end up with dozens of variables to keep track of your Tk widgets, and you may be at risk for clashes and hard-to-debug behaviour!

A better approach

Here is an improved version that implements all four points raised here above:

The button has now much more space around it. On Windows, it looks native, but on Linux it is still looking old-fashioned…

Our code is now becoming quite complicated. However, the tcltk2 package would be helpful here.

The tcltk2 version

The tcltk2 R package4 provides more advanced Tk widgets, additional R-Tcl commands, more modern themes for Linux and Mac OS X and it simplifies the creation of GUI items. Here is how you could get the same window using tcltk2:

The default theme on Linux is clearlooks, which gives the next visual:

1. A container is a widget that can contain other widgets. A toplevel window like win1 is a container, while butOK is not.

2. Never mix Tk managers inside the same container!

3. Rerun the previous code to recreate the window and the button. Do not close that window, but rerun win1 <- tktoplevel(). This will create a new window, as win1. Now, when you click the OK button on the first window, it is the second window that is destroyed!

4. Install the package from CRAN with the instruction install.packages("tcltk2")