0

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"))
Stedy
  • 7,359
  • 14
  • 57
  • 77

1 Answers1

0

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)
})
jverzani
  • 5,600
  • 2
  • 21
  • 17
  • Thanks this works! I was unaware of the (un)blockHandler. This will come in handy as I have several convoluted widgets in my GUI. In gWidgets, would it be preferable that handlers be executed only upon widget selection and not widget assignment too? Thanks again for your help. – user2211814 Mar 27 '13 at 11:10
  • Yes, in gWidgetsRGtk2 I try to set the value to the old value when you assign new items to a combobox. It is a convenience to the programmer. In your example, this selection can be done so there is selection going on. I didn't do this in gWidgets2RGtk2 -- I now agree it would be preferable to have the behavior you describe. – jverzani Mar 27 '13 at 20:09