0

I'm working on a tcltk interface in R that creates a set of drop down combo boxes based on the fields in whichever dataset is loaded by the user. Right now I have a button ("getbox") to press after loading the data that reads the data then creates a combo box with an option for each field. I'd like to be able to re-create the combo box each time I press the button instead of adding a new combo box. Here is an example of what I have:

tt <- tktoplevel()
comboframe <- tkframe(tt)

   getbox <- tkbutton(tt, text = "Create Combo Box", command = function() {
        fields <- names(mtcars)
        cbox <- tkwidget(comboframe, "ComboBox", editable = FALSE, values = fields)
        tkgrid(cbox) 
        })

   tkgrid(getbox)
   tkgrid(comboframe)

I tried adding an if statement in the getbox command function, but couldn't get it to work.

if (exists(comboframe)) {
    tkdestroy(comboframe)
}  # then create combobox ...

Any thoughts on how to replace the combo box instead of adding a new one? Thanks!

6
  • I don't know R, but why are you checking if the frame comboframe exists? Why not if (exists(cbox)) {tkdestroy(cbox)} between creating the fields and creating the cbox?
    – Jerry
    Oct 1, 2015 at 5:43
  • I was checking for the frame instead of the combo box because I actually have six combo boxes in the real script and 'm trying to do the same thing also with a second button that creates one check box for each field in the dataset - the single combo box was a simpler example. I need the function to delete all the check boxes and all the combo boxes before re-creating them each time the button is pressed. I thought it would be simpler to delete the frame and thereby delete all the combo/check boxes included in the frame than to check for each box and delete it.
    – LM6
    Oct 1, 2015 at 23:11
  • ... but even the 'if (exists(cbox)) {tkdestroy(cbox)}' doesn't work - it gives the same error (Error in exists(cbox) : object 'cbox' not found)
    – LM6
    Oct 1, 2015 at 23:14
  • Ok. Sorry, I don't think I can help you more than that :(
    – Jerry
    Oct 2, 2015 at 7:10
  • 1
    Figured it out! It just wasn't finding the frame. Here is the corrected code: tt <- tktoplevel() getbox <- tkbutton(tt, text = "Create Combo Box", command = function() { if (exists('comboframe')) { tkdestroy(comboframe) } comboframe <<- tkframe(tt) fields <- names(mtcars) cbox <- tkwidget(comboframe, "ComboBox", editable = FALSE, values = fields) tkgrid(cbox) tkgrid(comboframe) }) tkgrid(getbox)
    – LM6
    Oct 7, 2015 at 22:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.