6.4. Radio Buttons

Radio buttons are similar to check buttons except they are grouped so that only one may be selected/depressed at a time. This is good for places in your application where you need to select from a short list of options.

Creating a new radio button is done with this call:

  radio_button = gtk.RadioButton(group=None, label=None)

You'll notice the extra argument to this call. Radio buttons require a group to operate properly. The first call to gtk.RadioButton() should pass None as the first argument and a new radio button group will be created with the new radio button as its only member.

To add more radio buttons to a group, pass in a reference to a radio button in group in subsequent calls to gtk.RadioButton().

If a label argument is specified the text will be parsed for '_'-prefixed mnemonic characters.

It is also a good idea to explicitly set which button should be the default depressed button with:


This is described in the section on toggle buttons, and works in exactly the same way. Once the radio buttons are grouped together, only one of the group may be active at a time. If the user clicks on one radio button, and then on another, the first radio button will first emit a "toggled" signal (to report becoming inactive), and then the second will emit its "toggled" signal (to report becoming active).

The example program radiobuttons.py creates a radio button group with three buttons. Figure 6.4, “Radio Buttons Example” illustrates the resulting window:

Figure 6.4. Radio Buttons Example

Radio Buttons Example

The source code for the example program is:

    1	#!/usr/bin/env python
    3	# example radiobuttons.py
    5	import pygtk
    6	pygtk.require('2.0')
    7	import gtk
    9	class RadioButtons:
   10	    def callback(self, widget, data=None):
   11	        print "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()])
   13	    def close_application(self, widget, event, data=None):
   14	        gtk.main_quit()
   15	        return False
   17	    def __init__(self):
   18	        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
   20	        self.window.connect("delete_event", self.close_application)
   22	        self.window.set_title("radio buttons")
   23	        self.window.set_border_width(0)
   25	        box1 = gtk.VBox(False, 0)
   26	        self.window.add(box1)
   27	        box1.show()
   29	        box2 = gtk.VBox(False, 10)
   30	        box2.set_border_width(10)
   31	        box1.pack_start(box2, True, True, 0)
   32	        box2.show()
   34	        button = gtk.RadioButton(None, "radio button1")
   35	        button.connect("toggled", self.callback, "radio button 1")
   36	        box2.pack_start(button, True, True, 0)
   37	        button.show()
   39	        button = gtk.RadioButton(button, "radio button2")
   40	        button.connect("toggled", self.callback, "radio button 2")
   41	        button.set_active(True)
   42	        box2.pack_start(button, True, True, 0)
   43	        button.show()
   45	        button = gtk.RadioButton(button, "radio button3")
   46	        button.connect("toggled", self.callback, "radio button 3")
   47	        box2.pack_start(button, True, True, 0)
   48	        button.show()
   50	        separator = gtk.HSeparator()
   51	        box1.pack_start(separator, False, True, 0)
   52	        separator.show()
   54	        box2 = gtk.VBox(False, 10)
   55	        box2.set_border_width(10)
   56	        box1.pack_start(box2, False, True, 0)
   57	        box2.show()
   59	        button = gtk.Button("close")
   60	        button.connect_object("clicked", self.close_application, self.window,
   61	                              None)
   62	        box2.pack_start(button, True, True, 0)
   63	        button.set_flags(gtk.CAN_DEFAULT)
   64	        button.grab_default()
   65	        button.show()
   66	        self.window.show()
   68	def main():
   69	    gtk.main()
   70	    return 0        
   72	if __name__ == "__main__":
   73	    RadioButtons()
   74	    main()

The code is fairly straight forward. Lines 63-64 make the "close" button the default widget so that pressing the "Enter" key when the window is active causes the "close" button to emit the "clicked" signal.