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 button is done with one of these calls:

These are explained below.

To achieve the desired effect (that only one radio button can be checked at one time) all the buttons in question must belong to the same group. The original way to accomplish this is:

  1. Create a radio button.

  2. Obtain the group of this radio button.

  3. Create the next button, using the group as an argument.

The important thing to remember is that, every time you want to add a new button, you must obtain the group again, from the last button that you created. The result looks like this:

# Original button.
button[0] = "Button 0"

# Subsequent buttons.
group = button[0].group
button[1] = group, "Button 1"

group = button[1].group
button[2] = group, "button 2"

In practice, we never really need the group for any reason other than creating a new button. So we might as well chain these together:

# Original button.
button[0] = "Button 0"

# Subsequent buttons.
button[1] = button[0].group, "Button 1"
button[2] = button[1].group, "button 2"

Because this construct is so common, Gtk+ also has the option of simply passing the button. Like so:

# Original button.
button[0] = "Button 0"

# Subsequent buttons.
button[1] = button[0], "Button 1"
button[2] = button[1], "button 2"

In most cases, this will be your best option.

It is also a good idea to explicitly set which button should be the default depressed button with button.set_active(true)

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 following example might help make things clear.

Figure 2. Radio buttons

require 'gtk'

window.signal_connect('delete_event'){ Gtk.main_quit }
window.set_title    "Radio Buttons"

vbox =
vbox.border_width 10
window.add vbox

# Original button.
button = "button 1"
vbox.pack_start button

# Subsequent buttons.
button = button, "button 2"
vbox.pack_start button

button = button, "button 3"
vbox.pack_start button

button = button, "button 4"
vbox.pack_start button

# Done