带R Tcl / Tk的简单对话框

时间:2012-08-08 21:16:08

标签: r tcl tk

我尝试用Tcl小部件对话框调用一个简单的函数 - 把x,y和z加起来,在控制台中显示结果,并带有一个消息框(“完成!”) - 但是我错过了一些变量,并没有设法做消息框..

mydialog <- function(){

       myfun <- function(x,y,z) x+y+z

       xvar <- tclVar("1")
       yvar <- tclVar("2")
       zvar <- tclVar("3")

       tt <- tktoplevel()
       tkwm.title(tt,"MYTEST")
       x.entry <- tkentry(tt, textvariable=xvar)
       y.entry <- tkentry(tt, textvariable=yvar)
       z.entry <- tkentry(tt, textvariable=zvar)

       reset <- function()
        {
         tclvalue(xvar)<-""
         tclvalue(yvar)<-""
         tclvalue(zvar)<-""
        }

       reset.but <- tkbutton(tt, text="Reset", command=reset)

       x <- tclvalue(xvar)
       y <- tclvalue(yvar)
       z <- tclvalue(zvar)
       submit.but <- tkbutton(tt, text="submit", command=function() substitute(myfun(x,y,z)))

       tkgrid(tklabel(tt,text="MYTEST"),columnspan=2)
       tkgrid(tklabel(tt,text="x variable"), x.entry)
       tkgrid(tklabel(tt,text="y variable"), y.entry)
       tkgrid(tklabel(tt,text="z variable"), z.entry)
       tkgrid(submit.but, reset.but)

    }

mydialog()

1 个答案:

答案 0 :(得分:2)

我看不到您myfun的定义位置。这是你的代码略有改动

mydialog <- function(){

       xvar <- tclVar("1")
       yvar <- tclVar("2")
       zvar <- tclVar("3")

       tt <- tktoplevel()
       tkwm.title(tt,"MYTEST")
       x.entry <- tkentry(tt, textvariable=xvar)
       y.entry <- tkentry(tt, textvariable=yvar)
       z.entry <- tkentry(tt, textvariable=zvar)

       reset <- function()
        {
         tclvalue(xvar)<-""
         tclvalue(yvar)<-""
         tclvalue(zvar)<-""
        }

       reset.but <- tkbutton(tt, text="Reset", command=reset)

       submit <- function() {
         x <- as.numeric(tclvalue(xvar))
         y <- as.numeric(tclvalue(yvar))
         z <- as.numeric(tclvalue(zvar))
         print(x+y+z)
         tkmessageBox(message="Done!")
         #tkdestroy(tt)
       }
       submit.but <- tkbutton(tt, text="submit", command=submit)

       tkgrid(tklabel(tt,text="MYTEST"),columnspan=2)
       tkgrid(tklabel(tt,text="x variable"), x.entry)
       tkgrid(tklabel(tt,text="y variable"), y.entry)
       tkgrid(tklabel(tt,text="z variable"), z.entry)
       tkgrid(submit.but, reset.but)

    }

mydialog()