c – GTK3 – radiobutton: callback is called twice every time toggle the button-ThrowExceptions

Exception or error:

I’m trying to use gtk3 radiobutton following the turorial bellow. The problem is I noticed the callback funcion is being called twice everytime I click in the button.
My question is why and how can I change this for the callback to be called one time when I click in a radio button?


#include <gtk/gtk.h>

static void foo(GtkWidget *widget, gpointer data)
    char *id_radio = (char*)data;
    g_print("%s\n", id_radio);

int main(int argc, char *argv[])
    gtk_init(&argc, & argv);

    GtkWidget *radio_1, *radio_2, *radio_3, *vbox, *window;

    radio_1 = gtk_radio_button_new_with_label(NULL, "Radio 01");
    radio_2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_1), "Radio 02");
    radio_3 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_1), "Radio 03");
    vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);                      
    g_signal_connect(GTK_TOGGLE_BUTTON(radio_1), "toggled", G_CALLBACK(foo), (gpointer)"1");
    g_signal_connect(GTK_TOGGLE_BUTTON(radio_2), "toggled", G_CALLBACK(foo), (gpointer)"2");
    g_signal_connect(GTK_TOGGLE_BUTTON(radio_3), "toggled", G_CALLBACK(foo), (gpointer)"3");

    gtk_box_pack_start(GTK_BOX(vbox), radio_1, 1, 1, 0);
    gtk_box_pack_start(GTK_BOX(vbox), radio_2, 1, 1, 0);
    gtk_box_pack_start(GTK_BOX(vbox), radio_3, 1, 1, 0);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    return 0;
How to solve:

The “toggled” signal which is inherited from a toggle button, triggers both on deactivation and activation. when you select a radio button, the previously selected button gets deactivated (the first callback), and the newly pressed one activates (the second callback).

If you want to filter out just the activations, get the toggle button’s state inside the callback with gtk_toggle_button_get_active ().

As per documentation from:


Leave a Reply

Your email address will not be published. Required fields are marked *