2012-01-11 2 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을 호출하는 경우에만 필요합니다. 당신이 해달라고하면

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

은의 버튼의 세리 만들어 보자 :

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) 

을하고 모든 버튼은 동일한 핸들러에 연결되어 여전히 연결을 사용해야하지만, 읽기가 쉽습니다.

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, 처리 할 메서드가없고, 그 자체로 제한하지 않아도됩니다. 더 유연하게 사용해야하는 경우 익명 함수를 사용할 수도 있습니다. 물론 그 다음 단계는 메서드를 사용하는 것입니다. 불행히도 그들은 단순한 .Checked 속성을 제공하지 못했습니다. 다음 제안은 라디오 버튼 자체를 재정의하고 토글 된 상태가 변경되면 MFC, Qt 및 Winforms ... 등의 다른 프레임 워크를 에뮬레이트하는 Checked 속성을 연결하는 것입니다.

추신 : 단순화를 위해 보편적 인 코드를 생략 했으므로 좀 더 혼란스러운 답변을 만들 수 있습니다. 실제로 생성자를 올바르게 호출 할 수 있는지 여부에 대한 데모가 아닌 사실을 원할 수도 있습니다.

0

사용 람다 표현식 주위 성가신 방법과 혼란에 원하는 :

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

구글은 파이썬/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)); 
    } 
} 
관련 문제