<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Change a label in GTK+ 2' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Change a label in GTK+ 2' posted on the 'LINUX programming' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sun, 19 May 2013 04:18:01 -0700</pubDate>
    <lastBuildDate>Sun, 19 May 2013 04:18:01 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Change a label in GTK+ 2</title>
      <link>http://www.programmersheaven.com/mb/Linux/366036/366036/change-a-label-in-gtk+-2/</link>
      <description>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 gtk.org. 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&lt;br /&gt;
&lt;br /&gt;
Here is my code so far.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
#include &amp;lt;gtk/gtk.h&amp;gt;
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.\n");
    else g_print (" and %.0f people think C is uncool.\n", n);
    g_print ("%s\n",coolness);
}

/* 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 ;
    sprintf(coolness,"%.2f",p);
}
/*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 (&amp;amp;argc, &amp;amp;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;
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Thanks :)&lt;br /&gt;
PS - I am new to C as well.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Linux/366036/366036/change-a-label-in-gtk+-2/</guid>
      <pubDate>Sun, 30 Sep 2007 00:33:21 -0700</pubDate>
      <category>LINUX programming</category>
    </item>
    <item>
      <title>Re: Change a label in GTK+ 2</title>
      <link>http://www.programmersheaven.com/mb/Linux/366036/374919/re-change-a-label-in-gtk+-2/#374919</link>
      <description>: static void updatelabel2(void)&lt;br /&gt;
: {&lt;br /&gt;
: void gtk_label_set_text( GtkLabel   *label2,&lt;br /&gt;
:                          const char *coolness );&lt;br /&gt;
: }&lt;br /&gt;
Here's the problem.  You declared gtk_label_set_text instead of calling it.  Change it to:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
static void updatedlabel2(void)
{
    gtk_label_set_text(label2,coolness);
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
There--problem fixed.&lt;br /&gt;
&lt;br /&gt;
Patrick&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Linux/366036/374919/re-change-a-label-in-gtk+-2/#374919</guid>
      <pubDate>Fri, 05 Sep 2008 09:53:20 -0700</pubDate>
      <category>LINUX programming</category>
    </item>
  </channel>
</rss>