The following code creates a toplevel widget with a Tk canvas showing the currently selected color as its background, and a button which can be used to change the color.
require(tcltk)
tt <- tktoplevel()
tkwm.title(tt,"Color Selection")
color <- "blue"
canvas <- tkcanvas(tt,width="80",height="25",bg=color)
ChangeColor <- function()
{
color <- tclvalue(tkcmd("tk_chooseColor",initialcolor=color,title="Choose a color"))
if (nchar(color)>0)
tkconfigure(canvas,bg=color)
}
ChangeColor.button <- tkbutton(tt,text="Change Color",command=ChangeColor)
tkgrid(canvas,ChangeColor.button)
Clicking on the "Change Color" button gives the color-selection widget:
Change the color from blue to red:
Now the color is updated on the toplevel widget:
No R code will be shown here, but while we are discussing allowing the user to select a color, e.g. for points on a
scatter plot, we may also want to allow the user to select a plot symbol. One way to do this is to display all of
the plot symbols (see the example in help("points") in R) in a tkrplot window, and then ask the user to
click on one. (See the section on interactive tkrplot graphics.)