[R] ttkcombobox

Greg Snow Greg.Snow at imail.org
Tue Oct 28 03:14:27 CET 2008


Here is one example of using ttkcombobox, hopefully it helps:

library(tcltk)

have.ttk <- function () { # from Prof. Ripley
    as.character(tcl("info", "tclversion")) >= "8.5"
}


testfunc <- function() {
        tt <- tktoplevel()
        if( !have.ttk() ) stop( "Need version 8.5 or greater of tcl" )

        tkpack(tklabel(tt, text='Choose a package'), side='top')

        tmp <- tclVar()
        tclvalue(tmp) <- 'TeachingDemos'

        tkpack(ttkcombobox( tt, values=c('blockrand',
                'ObsSens','TeachingDemos','sudoku'), textvariable=tmp ),
                side='left' )

        tkpack(tkbutton(tt,text='print',command=function() print(tclvalue(tmp))))
        tkpack(tkbutton(tt,text='exit', command=function() tkdestroy(tt)),side='right')
}


testfunc()

If you want something to happen when you change the combobox (rather than just getting the value when you press a button or something else), then you will probably have to use tkbind to bind an event to it, or use tkafter to check it on a regular basis.  I usually do as above and just have a button or something else run the code when I want and it just gets the current value.

Hope this helps,


--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Sundar Dorai-Raj
> Sent: Monday, October 27, 2008 3:02 PM
> To: r-help at r-project.org
> Subject: [R] ttkcombobox
>
> Hi, all,
>
> (sessionInfo at the end)
>
> I've been struggling with the tcltk package and can't seem to get the
> ttkcombobox to work. Here's an example:
>
> library(tcltk)
> p <- tktoplevel()
> l <- tclVar()
> ## I don't know if I'm even calling it correctly
> cb <- ttkcombobox(p, values = letters[1:4], textvariable = l)
> tkpack(cb)
>
> 1. How do I know when the value of the combobox has been changed from,
> say, "a" to "b"?
> 2. How can I get the value of the box? E.g. if I switch the box to "b",
> how can I access this value?
> 3. How can I set the default value to something of my choice? I've
> tried
> the "set" argument as described in the "Tcl/Tk 8.5 Manual" but I
> received an error:
> cb <- ttkcombobox(p, values = letters[1:4], textvariable = l, set =
> "b")
> Error in structure(.External("dotTclObjv", objv, PACKAGE = "tcltk"),
> class = "tclObj") :
>    [tcl] unknown option "-set".
>
> Thanks,
>
> --sundar
>
>  > sessionInfo()
> R version 2.7.2 (2008-08-25)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
> States.1252;LC_MONETARY=English_United
> States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
>
> attached base packages:
> [1] tcltk     stats     graphics  grDevices utils     datasets  methods
> [8] base
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list