2

I've followed the TclTk example for creating a listbox to let the user choose their favorite fruit (see code below). The example prints the user's choice and that's it. However, I want to use that choice later in my script to do various things (use it in plot titles, filenames, etc.) How do I return the user's choice from a TclTk GUI to the rest of my script?

library(tcltk2)

win1 <- tktoplevel()
win1$env$lst <- tk2listbox(win1, height = 4, selectmode = "single")
tkgrid(tk2label(win1, text = "What's your favorite fruit?", justify = "left"),
padx = 10, pady =c(15, 5), sticky = "w")
tkgrid(win1$env$lst, padx = 10, pady = c(5, 10))
fruits <- c("Apple", "Orange", "Banana", "Pear", "Apricot")
for (fruit in fruits)
  tkinsert(win1$env$lst, "end", fruit)
# Default fruit is Banana.  Indexing starts at zero.
tkselection.set(win1$env$lst, 2)

onOK <- function() {
  fruitChoice <- fruits[as.numeric(tkcurselection(win1$env$lst)) + 1]
  tkdestroy(win1)
  msg <- paste0("Good choice! ", fruitChoice, "s are delicious!")
  tkmessageBox(message = msg)
}
win1$env$butOK <-tk2button(win1, text = "OK", width = -6, command = onOK)
tkgrid(win1$env$butOK, padx = 10, pady = c(5, 15))
jim616
  • 453
  • 1
  • 4
  • 6
  • 4
    While I'm really excited that somebody is building something with TclTk in R, you need to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), or at least what you've got so far and what packages you're using. – alistaire Sep 16 '16 at 01:58
  • My apologies. I've added the code for the example I was following. – jim616 Sep 16 '16 at 16:09

2 Answers2

2

How do I return the user's choice from a TclTk GUI to the rest of my script?

This depends on what you want to do

Option 1:

If you don't need the GUI anymore you destroy it after extracting the selected settings from the GUI in R variables. This is exactly what you do in the onOK "event handler" function.

You should add the following R command to wait for the tcltk window to be closed before continuing the execution of the R code after this line:

# Wait for the window to be closed
tkwait.window(win1)

You can then use the values in R variables (filled from the GUI settings in the event handler function) in your business logic e. g. by adding the business logic code at at the end of your code:

choice <- tkmessageBox(message=paste("Do you want to buy", fruitChoice, "now?"), type = "yesno", icon="question")

Option 2:

Your business logic is triggered (executed) in the event handler functions (without destroying/closing the window) to visualize the output ("state change") of the business logic.

In this GUI driven paradigm you add the business logic code into the event handler functions directly without calling tkdestroy.

For closing the windows you add "exit", "close", "cancel"... buttons. In the event handler function of these buttons you call tkdestroy.

R Yoda
  • 8,358
  • 2
  • 50
  • 87
0

There are command parameters available with listboxes (need to know which package you used for creating it). e.g there is a parameter "-selectioncommand". You can set a namespace variable to access the selected value.

e.g -selectioncommand {set ::xyz::test} this should be used while creating a widget.

Anuj Gupta
  • 125
  • 1
  • 8
  • Thanks. I've added the code for the example I was using. Where would I use the selectioncommand parameter? – jim616 Sep 16 '16 at 16:09
  • Is that a tcl? I'm not familiar with this format, try using in -> win1$env$lst <- tk2listbox(win1, height = 4, selectmode = "single") when you are creating the listbox – Anuj Gupta Sep 17 '16 at 10:09
  • That was the first example I found when Googling tcltk listbox for R. I'm unclear how to use the selection command you suggested. I'm not committed to tcltk or the format of the above example. Is there a more proper format to tcltk? – jim616 Sep 20 '16 at 19:26
  • ::iwidgets::Scrolledlistbox is the widget name and link is "http://incrtcl.sourceforge.net/iwidgets/manpages/iwidgets3.0/scrolledlistbox.n.html". This should help you. – Anuj Gupta Sep 21 '16 at 04:32