2008-03-03
Quick Tip - GTK RadioToolButton With All Buttons Unset
In case you were unaware, GTK+ doesn’t let you set_active(False) on every single button in a RadioToolButton group. One must be pressed at all times.
So, if you want to have a user interface where it’s possible to have none of them pressed (for example, using RadioToolButtons for “Zoom 100%” and “Zoom to Fit” and regular buttons for “Zoom In” and “Zoom Out”), you need to use a little trickery.
self.zoom_100 = self.wTree.get_widget("zoom_100")
self.zoom_fit = self.wTree.get_widget("zoom_fit")
self.zoom_manual = gtk.RadioToolButton(self.zoom_100)
zoom_100 and zoom_fit are the RadioToolButtons as defined in my Glade XML file. The trick is zoom_manual. It is now part of the button group, but I’m not adding it to the user interface.
Now, all I have to do is call self.zoom_manual.set_active(True) when I want to unset all of the other buttons. Not the intended use, I’m sure, but bad designs sometimes call for hacky solutions.
P.S. If you’re thinking of using ToggleToolButtons instead, don’t. GTK apparently doesn’t provide any way to tell the difference between a click and a programmatically-generated toggle.

The Quick Tip - GTK RadioToolButton With All Buttons Unset by Stephan Sokolow, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Share Alike 2.5 Canada License.

