r - R中使用tcltk的多个组合框

标签 r tk-toolkit combobox

我一直在尝试使用 tcltk 包在 R 中定义多个组合框,但无济于事。我正在使用下面的代码。我的灵感是 here ,但是我似乎不能只标记它们comboBox1,comboBox2等...所以我决定尝试将它们的输出值设置为一个向量...但它们的输出值不对我来说有什么意义……有什么想法吗?

非常感谢

require(tcltk)
tclRequire("BWidget")
tt <- tktoplevel()
tkgrid(tklabel(tt,text="What's your favorite fruits?"))
fruit <- c("Apple","Orange","Banana","Pear")
num <- c(0:3)
num.fruit <- cbind(num, fruit)
#####1st box
comboBox <- tkwidget(tt,"ComboBox",editable=FALSE,values=num.fruit[,2])
tkgrid(comboBox)
Cbox1<- comboBox
tkfocus(tt)

######2nd box

comboBox <- tkwidget(tt,"ComboBox",editable=FALSE,values=num.fruit[,2])
tkgrid(comboBox)
Cbox2 <- comboBox
###

##preliminary wrap-ip to pass to OnOK function
pref1 <- tcl(Cbox1,"getvalue")
pref2 <- tcl(Cbox2,"getvalue")

  Prefs <- c(pref1,pref2)
######action on OK button
OnOK <- function()
{
    fruitChoice <- fruits[as.numeric(tclvalue(tcl(Prefs,"getvalue")))+1]

    tkdestroy(tt)
    msg <- paste("Good choice! ",fruitChoice,"s are delicious!",sep="")
    tkmessageBox(title="Fruit Choice",message=msg)

}
OK.but <-tkbutton(tt,text="   OK   ",command=OnOK)
tkgrid(OK.but)
tkfocus(tt)

最佳答案

你为什么不直接使用 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:就我个人而言,我放弃了 tcltk,转而使用 RGtk2,在我看来,它更加灵活,您可以使用 Glade Interface Designer 直观地设计界面

关于r - R中使用tcltk的多个组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3482513/

相关文章:

r - 如何在R中生成六角形网格

r - 在双图中标记点

Ruby + Tk 的 Canvas 和形状出现问题

perl - Perl 中的字符串化结构

excel - 从组合框中选择下一项,然后单击 Excel VBA 按钮

r - R中的纬度经度坐标到州代码

r - 黄土更平滑的跨度参数的小平面ggplot

可调整大小的 Tk 窗口做得很好

c# - 如果我选择第一项,组合框不显示所选项目

silverlight-4.0 - Silverlight MVVM,停止 SelectionChanged 触发以响应 ItemsSource 重置