LINUX programming

Moderators: ITA
Number of threads: 1347
Number of posts: 2935

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Change a label in GTK+ 2 Posted by RobertH-AU on 30 Sept 2007 at 12:33 AM
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

Here is my code so far.
#include <gtk/gtk.h>
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 (&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.
Report
Re: Change a label in GTK+ 2 Posted by phorgan1 on 5 Sept 2008 at 9:53 AM
: 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)
{
    gtk_label_set_text(label2,coolness);
}


There--problem fixed.

Patrick



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.