Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GTK+ How do I find which radio button is selected?

The tutorial here http://developer.gnome.org/gtk-tutorial/2.90/x542.html shows how to set up the radio buttons, but neglects to tell you how to use them.

How do I then find which radio button is selected?

My solution:

Initialise radio buttons with:

rbutton1 = gtk_radio_button_new_with_label(NULL, "button1");
gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton1, TRUE, TRUE, 0);

rbuttonGroup = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton1)); /*not sure what I'd use this line for currently though*/
rbutton2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(rbutton1), "button 2"); 
gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton2, TRUE, TRUE, 0);

rbutton3 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(rbutton1), "button 3"); 
gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton3, TRUE, TRUE, 0);

And update a variable telling you which radio button is selected with this method:

        void checkRadioButtons()
{
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutton1))==TRUE) selectedRadioButton =1;
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutton2))==TRUE) selectedRadioButton =2;
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutton3))==TRUE) selectedRadioButton =3;
}
like image 910
dwjohnston Avatar asked Jan 11 '12 00:01

dwjohnston


People also ask

What is a selected radio button?

A radio button or option button is a graphical control element that allows the user to choose only one of a predefined set of mutually exclusive options. The singular property of a radio button makes it distinct from checkboxes, where the user can select and unselect any number of items.

What is the default event of radio button?

28.2 Radio Button Design-Time Considerations By default, the control returns a Boolean numeric value to indicate its state (0 for deselected and 1 for selected). However, you can change the return value for a radio button control with the Value property.

Should radio buttons have a default selection?

Always Offer a Default SelectionIn case of radio buttons this means that radio buttons should always have exactly one option pre-selected. Select the safest and most secure option (to prevent data loss).


2 Answers

Google brought me here for python / pygtk / pygtk3 searches, so I hope its okay that I post a pygtk solution:

def _resolve_radio(self, master_radio):
    active = next((
        radio for radio in
        master_radio.get_group()
        if radio.get_active()
    ))
    return active

This uses a generator to return the first (which should be the only) active radio box that is active.

like image 145
ThorSummoner Avatar answered Oct 01 '22 03:10

ThorSummoner


This is how I do it.

GtkRadioButton * radio_button;
GtkRadioButton * radio_button1;
GtkRadioButton * radio_button2;
...
GSList * tmp_list = gtk_radio_button_get_group (radio_button);//Get the group of them.
GtkToggleButton *tmp_button = NULL;//Create a temp toggle button.

while (tmp_list)//As long as we didn't reach the end of the group.
{
  tmp_button = tmp_list->data;//Get one of the buttons in the group.
  tmp_list = tmp_list->next;//Next time we're going to check this one.

  if (gtk_toggle_button_get_active(tmp_button))//Is this the one active?
    break;//Yes.

  tmp_button = NULL;//We've enumerated all of them, and none of them is active.
}
//Here. tmp_button holds the active one. NULL if none of them is active.

See the discussion here. I don't know if they will add this function into it (seems not).

like image 29
Wesley Avatar answered Oct 01 '22 02:10

Wesley