使用tcltk在R中创建多个组合框

使用tcltk在R中创建多个组合框,r,tk,combobox,R,Tk,Combobox,我一直在尝试使用tcltk包在R中定义多个组合框,但没有成功。我正在使用下面的代码。我的灵感是,但是我似乎不能仅仅给它们贴上comboBox1、comboBox2等标签。。。所以我决定尝试将它们的输出值设置为一个向量。。。但是他们的输出值对我来说毫无意义。。。有什么想法吗 非常感谢 require(tcltk) tclRequire("BWidget") tt <- tktoplevel() tkgrid(tklabel(tt,text="What's your favorite frui

我一直在尝试使用tcltk包在R中定义多个组合框,但没有成功。我正在使用下面的代码。我的灵感是,但是我似乎不能仅仅给它们贴上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)
require(tcltk)
TcleRequire(“BWidget”)

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)
require(tcltk)

tt如果您不想太参与tcltk,您可能会发现gwidtk更容易使用

library(gWidgets)
options(guiToolkit="tcltk") ## or RGtk2 or Qt

w <- gwindow("Multiple comboboxes")
tbl <- glayout(cont=w, horizontal=FALSE)

fruit <- c("Apple","Orange","Banana","Pear")


tbl[1,1] <- "Favorite fuits"
tbl[1,2] <- (cb1 <- gcombobox(fruit, cont=tbl))

tbl[2,1] <- "Other fruit?"
tbl[2,2] <- (cb2 <- gcombobox(fruit, cont=tbl))

tbl[3,2] <- (b <- gbutton("Ok", cont=tbl))

addHandlerClicked(b, handler=function(h,...) {
  cat(sprintf("You picked %s and %s\n", svalue(cb1), svalue(cb2)))
})
库(gWidgets)
选项(guiToolkit=“tcltk”)35;#或RGtk2或Qt

这就是我想做的,把组合框的值放到一个向量中,然后用它来做一些事情。但我似乎无法使用RGtk2或tcltk或Qt**编辑来点击addHandlerClicked工作:我使用在这个令人惊叹的文档站点上找到的install.packages(“gWidgetsRGtk2”,dep=TRUE)来完成它:Qt工具包实现在r-forge上:。它仍在开发中,但看起来比Tlctk版本要好得多。谢谢@nico这正是我在类似项目中所需要的。我希望tcltk/2包在某个地方有很好的文档,但如果有的话,我还没有找到。更糟糕的是,我在网上发现了几个简单不起作用的例子。我真希望几个小时前就能找到这个!