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:
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
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
Our Tk window and the way we manage it is indeed far from optimal. It can be ameliorated in four ways:
It would be nice to give a title to our Tk window. This can be done using
We could use ttk instead of tk widgets by replacing
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).
We should think about the size of the widgets and margins around them for a better layout. Our tiny
OKbutton in the middle of a small window is not that nice. So, let’s improve this.
It is nice to keep track of our Tk windows and widgets by assigning variables, like
butOKhere. However, these variables clutter our workspace. They also do not reflect the hierarchy.
butOKis embedded in
win1at the Tk level. It makes clean up more difficult once the window is destroyed: you must get rid of both
butOKto free memory from items that are not needed any more. Finally, if you have two windows, each with an
OKbutton, you should of course not call them both
butOK. Also, reassigning
win1before 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:
A container is a widget that can contain other widgets. A toplevel window like
win1is a container, while
butOKis not. ↩
Never mix Tk managers inside the same container! ↩
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
OKbutton on the first window, it is the second window that is destroyed! ↩