使用tcltk的多个组合框 [英] multiple comboboxes in R using tcltk

查看:300
本文介绍了使用tcltk的多个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用tcltk包定义R中的多个组合框,但无效。我使用下面的代码。我的灵感来自这里,但我似乎不能 他们的comboBox1,comboBox2等等,所以我决定尝试和设置他们的输出值到一个向量...但他们的输出值对我没有任何意义...任何想法

非常感谢

  require(tcltk)
tclRequire(BWidget)
tt < - tktoplevel()
tkgrid(tklabel(tt,text =你最喜欢的水果是什么?))
fruit< - c Apple,Orange,Banana,Pear)
num < - c(0:3)
num.fruit < - cbind(num,fruit)
#####第一框
comboBox< - tkwidget(tt,ComboBox,editable = FALSE,values = num.fruit [,2])
tkgrid(comboBox)
Cbox1< ; - comboBox
tkfocus(tt)

######第二个框

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

##初步wrap-ip传递给OnOK函数
pref1 <-tcl(Cbox1,getvalue)
pref2 <-tcl(Cbox2,getvalue)

Prefs < - c pref1,pref2)
######对OK按钮的操作
OnOK< - function()
{
fruitChoice< - fruits [as.numeric(tclvalue tcl(Prefs,getvalue)))+ 1]

tkdestroy(tt)
msg < ,fruitChoice,s delicious delicious!,sep =)
tkmessageBox(title =Fruit Choice,message = msg)

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


解决方案

为什么不使用 ttkcombobox

  require(tcltk)
tt< - tktoplevel()
tkwm.title(tt,Fruits! b $ b tkwm.geometry(tt,200x150 + 300 + 300)

onOK< - function()
{
fav< - tclvalue(favFruit)
worst< - tclvalue(worstFruit)

if(fav!=Choose one)
tkmessageBox(title =最喜欢的水果,message = paste is,fav))
if(worst!=Choose one)
tkmessageBox(title =Worst fruit,message = paste(你最喜欢的水果是

if(fav ==Choose one& worst ==Choose one)
tkmessageBox(title =Well ...,message =Select a fruit! )
}

label1 < - tklabel(tt,text =你最喜欢的水果是什么?)
label2< - tklabel(tt,text =What fruit you喜欢最少?)

水果< - c(选择一个,苹果,橙,香蕉,梨)
#两个组合框
favFruit< - tclVar(选择一个)
worstFruit< - tclVar(选择一个)

第一个盒子
组合。 1 <-ttkcombobox(tt,values = fruits,textvariable = favFruit,state =readonly)
#第二框
combo.2&l​​t; - ttkcombobox(tt,values = fruits,textvariable = worstFruit ,state =readonly)
#如果您需要在用户更改选择时执行某些操作,请使用
#tkbind(combo.1,<< ComboboxSelected>>,functionname)

OK.but< - tkbutton(tt,text =OK,command = onOK)

tkpack(label1,combo.1)
tkpack ,combo.2)
tkpack(OK.but)

tkfocus(tt)


b $ b

PS:个人来说,我放弃了 tcltk ,赞成 RGtk2 您可以使用Glade Interface Designer来设计界面


I have been trying to define multiple combo boxes in R using the tcltk package but to no avail. I am using the below code. My inspiration was here, however I can't seem to just label them comboBox1, comboBox2, etc... so I decided to try and set their output values into a vector... but their output values don't make any sense to me... any ideas out there?

many thanks

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)

解决方案

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

这篇关于使用tcltk的多个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆