Adding and changing text labels in a window in R TclTk

Text labels can easily be added to a toplevel window using the tklabel function. It is not necessary to assign the result of tklabel to a variable unless you will want to change the text later on.

require(tcltk)
tt <- tktoplevel()
tkgrid(tklabel(tt,text="This is a text label"))

The following code illustrates how the text in a text label can be changed:

require(tcltk)
tt <- tktoplevel()
labelText <- tclVar("This is a text label")
label1 <- tklabel(tt,text=tclvalue(labelText))
tkconfigure(label1,textvariable=labelText)
tkgrid(label1)
ChangeText <- function() tclvalue(labelText) <- "This text label has changed!"
ChangeText.but <- tkbutton(tt,text="Change text label",command=ChangeText)
tkgrid(ChangeText.but)
Running the R code above gives the following window:


Pressing the "Change text label" button, gives the following window: