How to return values from gWidgets and handlers?

后端 未结 3 600
半阙折子戏
半阙折子戏 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)
    

提交回复
热议问题