如何从gWidget和处理程序返回值? [英] How to return values from gWidgets and handlers?

查看:60
本文介绍了如何从gWidget和处理程序返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为R包开发GUI(使用gWidgets).我的计划是构建一个保存数据的主窗口,并为每个函数使用按钮调用小型gui包装器.不幸的是,我陷入了一个基本的(?)问题-我不知道如何传输数据.

I am trying to develop a GUI (using gWidgets) for an R package. My plan was to construct a main window holding the data, and with buttons calling small gui wrappers for each function. Unfortunately I am stuck on a basic(?) problem - I don't know how to transfer the data.

问题:

  • 如何在单独的窗口之间正确发送数据?
  • 如何从另一个窗口的处理程序中发送数据?

我的问题类似于: 使用gWidget在R中加载和保存变量,但是从我读过不建议使用.GlobalEnv.

My problem is similar to: Loading and saving variables in R with gWidgets, but from what I have read the use of .GlobalEnv is not recommended.

我还看到有人使用<<-运算符: http://www.mail-archive.com/r-sig-gui@r-project.org/msg00053.html ,但我无法正确复制它(并且它我认为我的示例将无法正常工作.)

I have also seen someone using the <<- operator: http://www.mail-archive.com/r-sig-gui@r-project.org/msg00053.html, but I can't reproduce it properly (and it will not work with my example, I think).

下面是一个简单的示例,在该示例中,我尝试将文本发送到另一个窗口,如果按下该按钮,则再次发送文本.我已经尝试过在处理程序中使用return,但这是行不通的(也不确定是否允许).在处理程序/内部函数可以对数据执行操作之前,子窗口将在函数末尾立即返回其值.我不知道如何从处理程序中到达主窗口.

Below is a simple example, where I try to send a text to another window and back again if the button is pressed. I have tried with return inside the handler, but that doesn't work (also not sure if it is allowed). The subwindow immediately return its value at the end of the function, before the handler/inner function can act on the data. I don't know how to reach out from the handler to the main window.

main <- function(){

  library(gWidgets)
  options(guiToolkit="RGtk2")

  w <- gwindow(title="Main window",
               visible=FALSE)

  txt <- gtext(text="Initial text in main window.",
               container=w)

  btn <- gbutton("Send to sub window", container=w)

  addHandlerChanged(btn, handler = function(h, ...) {
    shouldbenew <- subwindow(svalue(txt))
    svalue(txt) <- paste("The sub window immediately returns 'TRUE', before pushing 'Return to main':", shouldbenew )
  } )

  visible(w) <- TRUE

}

subwindow<- function(text){

  library(gWidgets)
  options(guiToolkit="RGtk2")

  sw <- gwindow(title="Sub window",
                visible=FALSE)

  editedtxt <- gtext(text=paste(text, "- Text is transferred to the sub window, but I don't know how to send it back to the main window"),
                     container=sw)

  btn <- gbutton("Send to main window", container=sw)

  addHandlerChanged(btn, handler = function(h, ...) {
    newtxt <- svalue(editedtxt)
    return(newtxt)

  } )

  visible(sw) <- TRUE

}

更新: 这是我选择的解决方案(由jverzani建议),使用上面的示例进行了说明.我希望我正确理解了建议的解决方案,并且已经以不错"的方式实施了该方法,理想情况下已在CRAN上接受.

Update: Here is the solution I picked as the way forward (as suggested by jverzani), illustrated using the example above. I hope I understood the suggested solution correct and that I have implemented it in a 'nice' way, ideally accepted at CRAN.

总而言之,我在主窗口环境中创建了一个新环境.我编辑了子窗口,以便在调用中使用环境.在子窗口assign中按按钮,将已编辑的文本传递到所传递的环境.当子窗口关闭并且主窗口成为焦点时,可以使用get从环境中访问编辑后的文本.

To summarise I created a new environment within the main window environment. I edited the sub window to take the environment in the call. Pressing the button in the sub window assign the edited text to the passed environment. When the sub window is closed, and the main window comes into focus, the edited text is accessible from the environment using get.

main <- function(){

  library(gWidgets)
  options(guiToolkit="RGtk2")
  # Create a new environment for passing data.
  .mainGlobal <- new.env()

  w <- gwindow(title="Main window", visible=FALSE)

  txt <- gtext(text="Initial text in main window.",
               container=w)

  btn <- gbutton("Send to sub window", container=w)

  addHandlerChanged(btn, handler = function(h, ...) {
    # Call sub widget passing text and environment.
    subwindow(text=svalue(txt), env=.mainGlobal)
  } )

  visible(w) <- TRUE

  addHandlerFocus(w, handler = function (h, ...) {

    if(exists("myText", envir=.mainGlobal)){
      # Retrieve text and update.
      svalue(txt) <- get("myText", envir=.mainGlobal)
    }    
  })

}

subwindow<- function(text, env=NULL){

  library(gWidgets)
  options(guiToolkit="RGtk2")

  sw <- gwindow(title="Sub window", visible=FALSE)

  editedtxt <- gtext(text=text, container=sw)

  btn <- gbutton("Send to main window", container=sw)

  addHandlerChanged(btn, handler = function(h, ...) {
    newtxt <- svalue(editedtxt)
    assign("myText", newtxt, envir=env)
  } )

  visible(sw) <- TRUE

}

推荐答案

一种更好的方法是将GUI存储在引用类中,但这需要对代码进行更大的修改.

A better approach, but one that involves a bigger reworking of your code, is to store GUIs in reference classes.

您使用字段列表(每个小部件一个)调用setRefClass,并定义用于创建GUI的初始化方法.我通常创建一个函数来包装创建实例的调用.请参见代码块末尾的setRefClass.

You call setRefClass with a list of fields (one for each widget), and define an initialise method where the GUI is created. I usually create a function to wrap the call that creates an instance. See setRefClass at the end of the code block.

mainGui <- suppressWarnings(setRefClass( #Warnings about local assignment not relevant 
  "mainGui",
  fields = list(
    #widgets
    w   = "ANY",       #"GWindow"
    txt = "ANY",       #"GEdit"
    btn = "ANY"        #"GButton"
  ),
  methods = list(
    initialize = function(windowPosition = c(0, 0))
    {
      "Creates the GUI"

      w <<- gwindow(
        "Main window", 
        visible = FALSE,
        parent  = windowPosition
      )

      txt <<- gedit(
        "Initial text in main window.", 
        container = w
      )      

      btn <<- gbutton(
        "Send to sub window", 
        container = w
      )      

      addHandlerChanged(
        btn, 
        handler = function(h, ...) {
          subWindow$setText(getText())
        } 
      )

      visible(w) <- TRUE      
    },
    #other methods to access GUI functionality go here
    getText = function() 
    {
      svalue(txt)
    },
    setText = function(newTxt) 
    {
      svalue(txt) <- newTxt
    }
  )
))  

createMainGui <- function(...)
{
  invisible(mainGui$new(...))
}    

这篇关于如何从gWidget和处理程序返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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