Why don't you just use ttkcombobox
?
require(tcltk)
tt <- tktoplevel()
tkwm.title(tt, "Fruits!")
tkwm.geometry(tt, "200x150+300+300")
onOK <- function()
{
fav <- tclvalue(favFruit)
worst <- tclvalue(worstFruit)
if (fav != "Choose one")
tkmessageBox(title="Favorite fruit", message = paste("Your favorite fruit is", fav))
if (worst != "Choose one")
tkmessageBox(title="Worst fruit", message = paste("The fruit you like the least is", worst))
if (fav == "Choose one" & worst == "Choose one")
tkmessageBox(title="Well...", message = "Select a fruit!")
}
label1 <- tklabel(tt, text="What's your favorite fruit?")
label2 <- tklabel(tt, text="What fruit you like the least?")
fruits <- c("Choose one", "Apple", "Orange", "Banana", "Pear")
# Default selections for the two combo boxes
favFruit <- tclVar("Choose one")
worstFruit <- tclVar("Choose one")
# 1st box
combo.1 <- ttkcombobox(tt, values=fruits, textvariable=favFruit, state="readonly")
# 2nd box
combo.2 <- ttkcombobox(tt, values=fruits, textvariable=worstFruit, state="readonly")
# If you need to do something when the user changes selection just use
# tkbind(combo.1, "<<ComboboxSelected>>", functionname)
OK.but <- tkbutton(tt,text=" OK ", command = onOK)
tkpack(label1, combo.1)
tkpack(label2, combo.2)
tkpack(OK.but)
tkfocus(tt)
PS: personally, I abandoned tcltk
in favour of RGtk2
, much more flexible in my opinion and you can design interfaces visually using Glade Interface Designer
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…