Search code examples
rtk-toolkittcltk

R - Update text entry box (tkentry) based on value selected in dropdown (ttkcombobox)


I'm still quite new to R and tcltk / GUI programming.

Here is a simple GUI and what I would like to happen is for the text entry box to change to a different value depending on what the user selects from the dropdown menu.

I've tried searching for answers, but I've only found hints (tkbind, tkafter), which isn't enough for me to know how to actually write the code.

How is this done?

Many thanks!

UPDATE - Found the solution - a combination of tkbind and tkconfigure - scroll down to near the end of the code, where I've updated it with a working solution.

UPDATE2 - OK, so although this seems to work, it breaks my later code and doesn't actually use those values once the "OK" button is pressed. Added that code below. Any ideas? i.e. VariableToUse always stays at 10, even though I'm setting it to 1 later in the code when the combobox value is selected.

UPDATE3 - OK, fixed that too! It was a "level" thing I'd read about somewhere else - I just needed to use <<- instead of <-. Code updated below.

if (!require("tcltk2")) {
  install.packages("tcltk2", dependencies = TRUE)
  library(tcltk2)
}

#########################################################
### Parameters
#########################################################
MyEditBox <- tclVar(10)
# Default selection in dropdown
MyComboBox <- tclVar(1)
# Ok button
ret_var <- tclVar("")

#########################################################
### Button functions
#########################################################

submit <- function() {
  tclvalue(ret_var) <- "OK"
  tkdestroy(tt)
}

quit <- function() {
  tkdestroy(tt)
}


#########################################################
### BUILD GUI
#########################################################

## head line
tt <- tktoplevel(borderwidth = 20)
tkwm.title(tt, "ComboBox Test")

if(.Platform$OS.type == "windows"){
  box_length <- 63
}else{
  box_length <- 55
}
cell_width <- 3
bt_width <- 8

# Dropdown box
comboBox <- ttkcombobox(tt,values=c(1,2,3), textvariable = MyComboBox)

# Box1
box1 <- tkentry(tt, textvariable = MyEditBox, width = 6)


## submit / reset / quit 
submit_button <- tkbutton(tt, text = "Go ...", command = submit)
quit_button <- tkbutton(tt, text = "Quit", command = quit)


#########################################################
### Display GUI
#########################################################

tkgrid(tklabel(tt, text = ""),tklabel(tt, text = "Select a value:"))


tkgrid(tklabel(tt, text = "\n"), padx = cell_width)  # leave blank line

tkgrid(tklabel(tt, text = "Select Something:"), comboBox, padx = cell_width)

tkgrid(tklabel(tt, text = "\n"), padx = cell_width)  # leave blank line

tkgrid(tklabel(tt, text = "Box To Update:"), box1, padx = cell_width)
tkgrid(tklabel(tt, text = "\n"), padx = cell_width)  # leave blank line

### Go and Quit buttons
tkgrid(tklabel(tt, text = ""), submit_button, quit_button, padx = cell_width)

tkgrid.configure(quit_button, sticky = "w")

    tkbind(comboBox, "<<ComboboxSelected>>", function() {
       if (tclvalue(MyComboBox)==1){      
       # Change the value           
       MyEditBox <<- tclVar(1)
       # Update the GUI
       tkconfigure(box1,textvariable = MyEditBox)
      }
    })

tkwait.window(tt)


########################
### Go button pressed
########################

if (tclvalue(ret_var) == "OK") {
  VariableToUseLater <- tclvalue(MyEditBox)
}

Solution

  • Ok, I found the command I need:

    # Set the value
    MyEditBox <<- tclVar(1)
    # Show in GUI
    tkconfigure(box1, textvariable=MyEditBox)