如何触发按钮的处理程序?

时间:2021-04-15 18:54:46

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

如何在gWidgets2中触发按钮的处理程序?举个例子:

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.

我可以通过使用鼠标并“物理”点击按钮来触发b4的处理程序。但是如何通过一些R代码实现这一目标呢?我希望像activateHandler(b4)或类似的东西。

1 个解决方案

#1


1  

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

根据评论中的建议,我可以通过b4 $ invoke_change_handler()。或者通过将处理程序重新定义为单独的函数:

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

然后调用:h2()。

#1


1  

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

根据评论中的建议,我可以通过b4 $ invoke_change_handler()。或者通过将处理程序重新定义为单独的函数:

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

然后调用:h2()。