The date entry and calendar widgets

The date entry and calendar widgets

James Wettenhall & Philippe Grosjean bio photo By James Wettenhall & Philippe Grosjean

The Date Entry Widget

A basic date entry widget is available in the tcltk2 package. Here is how you can use it:


win1 <- tktoplevel()

# The variable that will contain my date
mydate <- tclVar()

# Use the datefiled widget (+ a label)
win1$env$date <- tkwidget(win1, "datefield::datefield",
textvariable = mydate) #, text = "11/23/2010")
tkgrid(tk2label(win1, text = "Enter a date:"), win1$env$date,
padx = 10, pady = 10)

# Initialize it at 11/24/2010 (MM/DD/YYYY)
tclvalue(mydate) <- "11/24/2010"

# Use an OK button to get the date
onOK <- function() {
print(as.Date(tclvalue(mydate), format = "%m/%d/%Y"))
win1$env$butOK <- tk2button(win1, text = "OK", width = -6, command = onOK)
tkgrid(win1$env$butOK, columnspan = 2, pady = 15)
tkbind(win1$env$date, "<Return>", onOK)

The code above produces the following date entry widget:

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 is in MM/DD/YYYY (and unfortunatelly cannot be modified). Change the date:

date entry widget modified

… then hit <Enter> or click the OK button, and you got:

## [1] "1999-05-15"

The Calendar Widget

Note: this has not been edited yet from the original form

This example was provided by Dirk Eddelbuettel.

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

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

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

## <Tcl>
## 07/01/2003