Question

The following code shows that assigning an item vector to gcombobox will result in looping of the gcombobox handler over each element of the existing item vector (try clicking, for example, "a" or "b" and you'll see the multiple printed messages from the gcombobox). If b2 is changed to a gradio button then this loop doesn't happen. Also, if the tcltk toolkit is used then we don't have an issue either. This is causing problems for me in a GUI where the handler for b2 is more complex and manipulates some large data. Any suggestions to prevent this looping would be great!

options("guiToolkit"="RGtk2")
library(gWidgets)

w=gwindow()
b1=gradio(c("a","b"),container=w)
b2=gcombobox(c(1:2),container=w)
addHandlerClicked(b1,handler=function(h,...) b2[,]=c(1:10))
addHandlerClicked(b2,handler=function(h,...) print("clicked b2"))
Was it helpful?

Solution

You can block the handlers then unblock as with:

w=gwindow()
b1=gradio(c("a","b"),container=w)
b2=gcombobox(c(1:2),container=w)
id = addHandlerClicked(b2,handler=function(h,...) print("clicked b2"))
addHandlerClicked(b1,handler=function(h,...) {
  blockHandler(b2, id)
  b2[,]=c(1:10)
  unblockHandler(b2, id)
})

I flipped the order of assignment to get the handler id.

Alternatively, in gWidgets2 (still just on Github) it just works, as this assignment of selectable items for b2 isn't setting the selected value of b2. (Which you might want to do manually)

options("guiToolkit"="RGtk2")
library(gWidgets2)

w=gwindow()
g = ggroup(cont=w)  ## only one child for a gwindow instance is enforced
b1=gradio(c("a","b"),container=g)
b2=gcombobox(c(1:2),container=g, expand=TRUE)
id = addHandlerChanged(b2,handler=function(h,...) print("clicked b2"))
addHandlerChanged(b1,handler=function(h,...) {
  b2[]=c(1:10)
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top