Search code examples
rbuttonuser-interfacehandlergwidgets

How to trigger the handler of a button?


How is it possible to trigger the handler of a button in gWidgets2? Take this example:

w <- gwindow("Buttons", visible=FALSE)
g <- ggroup(cont=w, horizontal=FALSE)

## various buttons

## without icon
b2 <- gbutton("ouvrir", cont=g)

## with a handler
b4 <- gbutton("click me", cont=g, handler=function(h,...) {
    if(svalue(b2) == "open")
        svalue(b2) <- "ouvrir"
    else
        svalue(b2) <- "open"
})

visible(w) <- TRUE

I can trigger the handler of b4 by using the mouse and "physically" clicking on the button. But how can I achieve this via some R code? I am hoping for something like activateHandler(b4) or similar.


Solution

  • As per the suggestions in the comments, I can either via b4$invoke_change_handler(). Or by redefining the handler as a separate function:

    w <- gwindow("Buttons", visible=FALSE)
    g <- ggroup(cont=w, horizontal=FALSE)
    ## various buttons
    ## without icon
    h2 <- function(h,...) {
    if(svalue(b2) == "open")
    svalue(b2) <- "ouvrir"
    else
    svalue(b2) <- "open"
    }
    b2 <- gbutton("ouvrir", cont=g)
    ## with a handler
    b4 <- gbutton("click me", cont=g, handler=h2)
    visible(w) <- TRUE
    

    and then calling that: h2().