c - GTK 中允许同时选中两个单选按钮

标签 c radio-button gtk

我正在尝试开发一个带有单选按钮的 GUI。单选按钮是在方法从文本文件中读取按钮名称后创建的,这些就是按钮的名称。

按钮已创建并正确显示。我的问题是两者同时被选中。怎么才能做到只允许同时检查一个呢?

static void sel_sets_prac()
{
    GtkWidget *window,*button,*vbox,*label,*frame;
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window,"delete-event",G_CALLBACK(gtk_main_quit),NULL);
    gtk_window_set_title(GTK_WINDOW(window), "ZHONG FC - Sets de Práctica");

    vbox = gtk_vbox_new(FALSE,3);

    frame = gtk_frame_new("Sets de Práctica");
    label = gtk_label_new("Escoge el set que desea practicar: ");
    gtk_container_add(GTK_CONTAINER(frame), label);
    gtk_box_pack_start(GTK_BOX(vbox),frame,TRUE,TRUE,0);

    //These variables are from other methods; I am pretty sure the problem is not here
    int num_sets = get_number_sets();
    char ** set_names;
    set_names = get_set_names(num_sets);

    GtkWidget *sets_select[num_sets];

    //I am guessing the problem is here, but I don't have a clue what to do
    int i;
    for(i = 0; i < num_sets; i++) {
        sets_select[i] = gtk_radio_button_new_with_label(NULL, set_names[i]);
        gtk_box_pack_start(GTK_BOX(vbox),sets_select[i],TRUE,TRUE,0);
    }

    gtk_container_set_border_width(GTK_CONTAINER(window), 50);
    gtk_container_add(GTK_CONTAINER(window),vbox);

    gtk_widget_show_all(window);
    gtk_main();
}

我不明白为什么允许同时选择多个按钮。请帮忙。

最佳答案

您的按钮位于不同的列表中,因此 GTK 不知道一次只允许其中一个按钮。您的问题是 gtk_radio_button_new_with_label 的第一个参数为 NULL;它需要指定一个按钮列表,以便将所有单选按钮视为一个组。你需要这样的东西(改编自 this tutorial ):

GtkWidget * button = gtk_radio_button_new_with_label (NULL, "button1");
GSList * group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
GtkWidget * button2 = gtk_radio_button_new_with_label(group, "button2");

关于c - GTK 中允许同时选中两个单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23691301/

相关文章:

c++ - 将一个字符串分成一个数组

php - 如何在分页期间收集单选按钮值?

python - 如何将 Gtk.StatusIcon 设置为文本

linux - 在 Scientific Linux 6.2 上更新 GTK+

c - 重绘 GtkWidget

c - 当您执行 "break function-name"时,GDB 如何确定要中断的地址?

c++ - 我可以将哪些同步原语用于 clone(2) (C/C++)?

c - 在 C 错误中翻译用户输入程序

javascript - 如果用户不接受 JS 确认消息,如何检查先前选择的单选按钮?

javascript - 与 VueJS 一起使用时无法选中单选按钮