Get data out of a tcltk function

ぐ巨炮叔叔 提交于 2019-11-29 10:54:37

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!