Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID


We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Change a label in GTK+ 2

Hello, I am learning C and decided to have a look into programming GUI's with GTK+, so i have been going through the GKT+ 2 tutorial at I have created an appliction from the two button demo to tally up votes. I want the string at the bottom of the box to change to the percent of positive votes. I cannot get the label's text to change. Does anybody know how this is done? or if I am using the wrong widget? Thanks -Robert

Here is my code so far.
float y = 0;
float n = 0;
float t = 0;
float p = 0;
GtkWidget *label2;
char coolness[20];
/* Our new improved callback. The data passed to this function
* is printed to stdout. */
static void callback( GtkWidget *widget,
gpointer data )
if(y==1) g_print ("1 person thinks C is cool,");
else g_print ("%.0f people think C is cool,", y);
if(n==1) g_print (" and 1 person thinks C is uncool.
else g_print (" and %.0f people think C is uncool.
", n);
g_print ("%s

/* another callback */
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
gtk_main_quit ();
return FALSE;
static void updown( GtkWidget *widget,
gint data)
if(data) y++;
else n++;
t = y + n;
p = 100 * y / t ;
/*void gtk_label_set_text( GtkLabel *label,
const char *str );*/
static void updatelabel2(void)
void gtk_label_set_text( GtkLabel *label2,
const char *coolness );

int main( int argc,
char *argv[] )

/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *button;
GtkWidget *box1;
GtkWidget *box0;
GtkWidget *label;

/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application. */
gtk_init (&argc, &argv);

/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

/* This is a new call, which just sets the title of our
* new window to "Please Vote" */
gtk_window_set_title (GTK_WINDOW (window), "Please Vote");

/* Here we just set a handler for delete_event that immediately
* exits GTK. */
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);

/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 10);

/* We create a box to pack widgets into. This is described in detail
* in the "packing" section. The box is not really visible, it
* is just used as a tool to arrange widgets. */
box0 = gtk_vbox_new (FALSE, 0);
box1 = gtk_hbox_new (FALSE, 0);

/* Put the box into the main window. */
gtk_container_add (GTK_CONTAINER (window), box0);

/* Creates a new button with the label "Yes". */
button = gtk_button_new_with_label ("Yes!");

/* Now when the button is clicked, we call the "callback" function
* with a pointer to "button 1" as its argument */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (updown), (gpointer) 1) ;

g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback), (gpointer) "is");

g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (updatelabel2), NULL);

/* Instead of gtk_container_add, we pack this button into the invisible
* box, which has been packed into the window. */
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);

/* Always remember this step, this tells GTK that our preparation for
* this button is complete, and it can now be displayed. */
gtk_widget_show (button);

/* Do these same steps again to create a second button */
button = gtk_button_new_with_label ("No!");

/* Call the same callback function with a different argument,
* passing a pointer to "button 2" instead. */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (updown), (gpointer) 0);

g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback), (gpointer) "isn't!");

g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (updatelabel2), NULL);

gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);

label = gtk_label_new ("Do you think C is cool?");

gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0);

gtk_widget_show (label);

gtk_box_pack_start (GTK_BOX (box0), label, FALSE, FALSE, 0);

gtk_box_pack_start(GTK_BOX (box0), box1, TRUE, TRUE, 0);

label2 = gtk_label_new ("C is cool!");

gtk_misc_set_alignment (GTK_MISC (label2), 0.5, 0);

gtk_box_pack_start(GTK_BOX (box0), label2, TRUE, TRUE, 0);

/* The order in which we show the buttons is not really important, but I
* recommend showing the window last, so it all pops up at once. */
gtk_widget_show (label2);

gtk_widget_show (button);

gtk_widget_show (box1);

gtk_widget_show (box0);

gtk_widget_show (window);

/* Rest in gtk_main and wait for the fun to begin! */
gtk_main ();

return 0;

Thanks :)
PS - I am new to C as well.


  • phorgan1phorgan1 Posts: 1Member
    : static void updatelabel2(void)
    : {
    : void gtk_label_set_text( GtkLabel *label2,
    : const char *coolness );
    : }
    Here's the problem. You declared gtk_label_set_text instead of calling it. Change it to:
    static void updatedlabel2(void)

    There--problem fixed.

Sign In or Register to comment.