[R] R and tcltk, problem to get the value entered

peter dalgaard pdalgd at gmail.com
Fri Dec 12 16:37:18 CET 2014

On 12 Dec 2014, at 11:02 , Jouanin Celine <celine_jouanin at yahoo.fr> wrote:

> Hi dear R list,
> I would like to create a graphical user interface with tcltk.I want to have a window where the user will enter a numeric, this will open a message box with his value. This works, but I don't succeed to get the value x, to use it after in a script in the R console. The x variable doesn't give me the value chosen by the user but returns the initial value "12".How can I do it ? Does exist an another function in tcltk that I need to use ?Thanks for your help
> Céline
> Here is my code :
> library(tcltk2)
> tt<-tktoplevel()
> tktitle(tt)<-"Select a value"
> titre<-tklabel(tt, text="Select a value")
> value<-tkentry(tt, width=3, textvariable=tclVar("12"))

I'd prefer this pattern of coding:

var <- tclVar("12")
entry <- tkentry(.... textvariable=var)

However, tkget() of an entry widget does return its value, so the direct cause of your trouble is different:

> ok<-function()
> {
>     show<-paste("The value is :", tclvalue(tkget(value)))
>     tkmessageBox(title="End", message=show, icon="info", type="ok")
>     tkdestroy(tt)
> }
> bouton<-tkbutton(tt, text="OK", command=ok)
> tkpack(titre)
> tkpack(value,bouton)
> x<-tclvalue(tkget(value))
> x<-as.numeric(x)
> x #this give 12 but I want to have the new value

I think this fetches the value before you get a chance to change it. You need to wait for it, either using tkwait.variable or tkwait.window. I believe that the tkttest demo has this structure.

Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com

More information about the R-help mailing list