How to return values from gWidgets and handlers?

后端 未结 3 637
半阙折子戏
半阙折子戏 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:40

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

提交回复
热议问题