How to return values from gWidgets and handlers?

后端 未结 3 601
半阙折子戏
半阙折子戏 2021-02-04 15:52

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 fun

相关标签:
3条回答
  • 2021-02-04 16:31

    You can pass information between gwidgets by using independent functions and without beforehand knowing the object name of the receiver widget:

    initMain <- function() {
      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)
    
      list(
        run = function(partner) {
          addHandlerChanged(btn, handler = function(h, ...) {
            svalue(partner$txt) <- svalue(txt)
          } )
          visible(w) <- TRUE
        },
        txt = txt
      )
    }
    
    initSubWindow<- function() {
      w <- gwindow(title="Sub window",visible=FALSE)
      txt <- gtext(text="huhu",container=w)
      btn <- gbutton("Send to main window", container=w)
    
      list(
        run = function(partner) {
          addHandlerChanged(btn, handler = function(h, ...) {
            svalue(partner$txt) <- svalue(txt)
          } )
          visible(w) <- TRUE
        },
        txt = txt
      )
    }
    
    mw <- initMain()
    sw <- initSubWindow()
    
    mw$run(sw)
    sw$run(mw)
    
    0 讨论(0)
  • 2021-02-04 16:40

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

    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(...))
    }    
    
    0 讨论(0)
  • You can just return the widgets for each window in a list. So the main function adds the line list(w = w, txt = txt, btn = btn) at the end to return each widget and make them accessible after the function has finished.

    The following example is the smallest change to your code that works , but there's a minor flaw in that main and subwindow now contain references to the return values from each other. The code works, but if you are doing something more complicated, it could easily become hard to maintain.

    library(gWidgets)
    options(guiToolkit="tcltk")
    
    main <- function(){
      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, ...) {
        svalue(subWindow$txt) <- svalue(mainWindow$txt)
      } )
    
      visible(w) <- TRUE
      list(w = w, txt = txt, btn = btn)
    }
    
    subwindow<- function(text){
      sw <- gwindow(title="Sub window",
                    visible=FALSE)
    
      editedtxt <- gtext(text="",
                         container=sw)
    
      btn <- gbutton("Send to main window", container=sw)
    
      addHandlerChanged(btn, handler = function(h, ...) {
        svalue(mainWindow$txt) <- svalue(subWindow$txt)
      } )
    
      visible(sw) <- TRUE
      list(w = sw, txt = editedtxt, btn = btn)
    }
    
    mainWindow <- main()
    subWindow <- subwindow()
    
    0 讨论(0)
提交回复
热议问题