I am creating a gbutton in gWidgets2 which will toggle between "go" and "stop" when it will be clicked.
I am following this example: toggling a group of icons in gWidgets
My Code:
library(gWidgets2)
library(gWidgets2RGtk2)
options(guiToolkit="RGtk2")
w= gwindow()
g1 <- ggroup(horizontal=TRUE, cont=w)
icon <- gbutton('go', container = g1)
state <- FALSE # a global
changeState <- function(h,...) {
if(state) {
svalue(icon) <- "go"
} else {
svalue(icon) <- "stop"
}
state <<- !state
}
addHandlerClicked(icon, handler=changeState)
It creates a button and toggle between "go" and "stop" when it is clicked. But the problem is I have to click two times to toggle. I want it should toggle between "go" and "stop" on one click.
You can use blockHandlers() and unblockHandlers() functions to avoid this issue.
w= gwindow()
g1 <- ggroup(horizontal=FALSE, cont=w)
icon <- gbutton("go", container = g1)
#icon <- gimage(reject,cont=g1)
state <- FALSE # a global
addHandlerClicked(icon, function(h,...) {
#
if(!state) {
blockHandlers(icon)
svalue(icon) <- "stop"
unblockHandlers(icon)
} else {
blockHandlers(icon)
svalue(icon) <- "go"
unblockHandlers(icon)
}
state <<- !state
})
I tried this and it works for me.