2012-01-11 54 views
3

這裏的教程http://developer.gnome.org/gtk-tutorial/2.90/x542.html 顯示瞭如何設置單選按鈕,但忽略了告訴你如何使用它們。GTK +如何找到選中哪個單選按鈕?

我該如何找到哪個單選按鈕?

我的解決辦法:

初始化單選按鈕有:

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); 

和更新變量,告訴你哪個單選按鈕是用這種方法選擇:

 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; 
} 

回答

0

您可以連接改爲GtkToggleButton::toggled信號。在關聯的回調中,您可以更新變量。至於撥打gtk_radio_button_get_group,您只需撥打gtk_radio_button_new_with_label而不是gtk_radio_button_new_with_label_with_widget,如您所引用的教程中指定的那樣。

0

讓我們創建按鈕的意甲:

for severity in levels: 
    radio = gtk.RadioButton(group=radioButtons, label=severity) 
    if severity == actualLevel: 
     radio.set_active(True) 
    hBox.pack_start(radio, True, True, 3) 
    radio.connect('toggled', self.radioButtonSelected, severity) 

,所有按鈕都連接到相同的處理程序:

def radioButtonSelected(self, button, currentSeverity): 
    # proceed with the task 
    # as you can see, button is passed by as argument by the event handler 
    # and you can, for example, get the button label : 
    labelReadFromButton = button.getLabel() 
0

使用lambda表達式,如果你不想惹煩方法周圍,仍然必須使用連接雖然,但其更容易閱讀:

Enum RadioValues { A, B, C, none }; 

RadioValues values = RadioValues.none; // only needed if you dont have an initially selected radio button 

MyConstructor() 
{ 
    Build(); 
    // asumming you have 3 radio buttons: radioA, radioB, radioC: 
    radioA.Toggled += (sender,e) => values = RadioValues.A; 
    radioB.Toggled += (sender,e) => values = RadioValues.B; 
    radioC.Toggled += (sender,e) => values = RadioValues.C; 

} 

和th ats它,沒有方法來處理,你也不必限制自己,如果你需要更多的flex,你也可以使用匿名函數 - 當然下一步是使用方法。不幸的是,他們並沒有提供簡單的.Checked屬性,我的下一個建議是重寫單選按鈕本身並在切換狀態發生變化時鏈接Checked屬性,模擬其他框架,如MFC,Qt和Winforms等。

PS:爲簡單起見,我遺漏了樣板代碼,這可能使答案更混亂一些,你可能只是希望事實不是一個示範,我是否可以正確地調用構造函數:)

1

這就是我做到這一點。

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. 

查看討論here。 我不知道他們是否將這個功能添加到它(似乎不)。

0

谷歌把我帶到這裏爲Python/PyGTK的/ pygtk3搜索,所以我希望它好,我張貼pygtk的解決方案:

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

它使用發電機來返回第一個(這應該只是)處於活動狀態的活動無線電框。

0

以下是我建議這樣做:

void radio_button_selected (GtkWidget *widget, gpointer data) 
{ 
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)) 
    { 
     GSLIST *group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget)); 
     g_print ("Index = %i%\n", g_slist_index (group, widget)); 
    } 
} 
相關問題