Get data out of a tcltk function

后端 未结 2 1917
余生分开走
余生分开走 2020-12-19 10:13

This is probably so simple I will cringe when the answer comes back but I am totally stumped. I have tried the manuals, tried searching the web, assorted examples and anythi

相关标签:
2条回答
  • 2020-12-19 10:33

    Here is a modification of your function:

    inputs <- function(){
    
       xvar <- tclVar("")
       yvar <- tclVar("")
    
       tt <- tktoplevel()
       tkwm.title(tt,"Input Numbers")
       x.entry <- tkentry(tt, textvariable=xvar)
       y.entry <- tkentry(tt, textvariable=yvar)
    
       reset <- function()
        {
         tclvalue(xvar)<-""
         tclvalue(yvar)<-""
        }
    
       reset.but <- tkbutton(tt, text="Reset", command=reset)
    
       submit <- function() {
         x <- as.numeric(tclvalue(xvar))
         y <- as.numeric(tclvalue(yvar))
         e <- parent.env(environment())
         e$x <- x
         e$y <- y
         tkdestroy(tt)
       }
       submit.but <- tkbutton(tt, text="submit", command=submit)
    
       tkgrid(tklabel(tt,text="Enter Two Inputs"),columnspan=2)
       tkgrid(tklabel(tt,text="Input1"), x.entry, pady = 10, padx =10)
       tkgrid(tklabel(tt,text="Input2"), y.entry, pady = 10, padx =10)
       tkgrid(submit.but, reset.but)
    
      tkwait.window(tt)
      return(c(x,y))
    }
    

    Now run the function like:

    myvals <- inputs()
    

    Now enter your 2 values and click "Submit", then look at the myvals variable, it contains your 2 values.

    0 讨论(0)
  • 2020-12-19 10:37

    You have them in the submit callback -- you just need to put them somewhere. Sometimes global variables are best for this. Just use <<- to assign to them so the bindings happen outside of the scope of the submit callback. You can also use an environment for this purpose or even a reference class.

    0 讨论(0)
提交回复
热议问题