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.

CC BY-SA 4.0 Quick Tip – GTK RadioToolButton With All Buttons Unset by Stephan Sokolow is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

This entry was posted in Geek Stuff. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution under the same terms as the associated post.

All comments are moderated. If your comment is generic enough to apply to any post, it will be assumed to be spam. Borderline comments will have their URL field erased before being approved.