从 tcltk 函数中获取数据 [英] Get data out of a tcltk function

查看:41
本文介绍了从 tcltk 函数中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能很简单,当答案回来时我会畏缩,但我完全被难住了.我尝试过手册,尝试搜索网络,各种示例以及我能想到的任何其他内容.我还是卡住了.

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 anything else I can think of. I am still stuck.

我正在尝试为用户创建一个简单的输入以添加两个值,然后我可以在 R 脚本的其余部分中使用.我需要脚本暂停并等待用户的输入,然后在获得输入后继续(例如选择文件功能的工作方式).在阅读了一堆东西之后,我决定使用库(tcltk).我在一个函数中有一个漂亮的小盒子.

I am trying to create a simple input for the user to add two values I can then use in the rest of the R script. I need the script to pause and wait for the input from the user and then continue along once it gets the input (like how the choose file function works). AFter reading a bunch of stuff I decided to use library(tcltk). I have a nice little box within a 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))
     print(x)
     print(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)

  }

当我输入时:

 inputs()

漂亮的小框弹出,我可以输入我的值,例如本例中的 3 和 4.

The nice little box pops up and I can input my values, say 3 and 4 for this example.

我回来了

<Tcl>  
[1] 3
[1] 4

我想在 R 代码的后续部分中使用这些数字.我如何获得它们才能获得与此等效的东西?

I want to use those number in a subsequent part of the R code. How do I get them so I can get the equivalent of this?

input1 <- 3
input2 <- 4

预先感谢您的帮助.

推荐答案

这里是对你的函数的修改:

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))
}

现在运行如下函数:

myvals <- inputs()

现在输入您的 2 个值并单击提交",然后查看 myvals 变量,它包含您的 2 个值.

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

这篇关于从 tcltk 函数中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆