The Date Entry and Calendar Widgets

The Date Entry Widget

This example was kindly contributed by Dirk Eddelbuettel.

The date entry widget is availabled as part of the Iwidgets package which does not come with the minimal Tcl/Tk installation bundled with R. If you want to use Iwidgets, install ActiveTcl and make sure that you have a TCL_LIBRARY environment variable set to your Tcl library path, e.g. C:\Tcl\lib\tcl8.4 and a MY_TCLTK environment variable set to a non-empty string, e.g. "Yes". If after doing this you still have problems with tclRequire("Iwidgets"), try addTclPath("C:\Tcl\lib").

## date entry
library(tcltk)
tclRequire("Iwidgets")
tt <- tktoplevel()
datefield <- tkwidget(tt, "iwidgets::datefield")
tkconfigure(datefield,
            labeltext="Please enter a date",
            command=function(...)cat(tclvalue(tkget(datefield))))
tkcmd(datefield, "show", "6/1/2002")
tkpack(datefield)
tkfocus(datefield)

The code above produces the following date entry widget. It does not let the user modify the slashes, only the numbers. It also ensures that the month is between 1 and 12 inclusive and that the day is between 1 and the number of days for that month, e.g. 31 for January. The date format in MM/DD/YYYY.

The Calendar Widget

This example was also provided by Dirk Eddelbuettel.

The calendar widget displays the current date and allows the user to click on a date to select it.

require(tcltk)
tclRequire("Iwidgets")
tt <- tktoplevel()
cal <- tkwidget(tt, "iwidgets::calendar")
tkconfigure(cal, command=function(...)cat(tclvalue(tkget(cal))))
tkpack(cal)

(This example was run on July 23rd). Clicking on July 1 gives:

<Tcl>
07/01/2003