GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Jul 28, 2014 10:31 pm

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Update Label Text at Runtime
PostPosted: Mon Oct 29, 2007 1:59 am 
Offline
Familiar Face

Joined: Mon Oct 29, 2007 1:32 am
Posts: 8
I am in the process of learning how to use GTK+ while writing a mission-critical application. What better time to do so, eh? I have experience with Visual Basic, but I'd sleep better at night knowing this application is running under Linux.

Anyways, I have several labels on the interface that I need to constantly update at runtime. I want to display the date, time, and other data. This application will be monitoring building temperature and humidity with the incorporation of a LabJack U3 module.

How exactly would I update a label's text at runtime?

Thank you,
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 4:12 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
All you have to do is call gtk_label_set_text(). This will replace the string that is displayed. You can use gtk_label_get_text() to retrieve the current text, but be careful because this is a "const char*", so it should not be changed!

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 7:20 pm 
Offline
Familiar Face

Joined: Mon Oct 29, 2007 1:32 am
Posts: 8
Thank you for the reply!

I found some examples online for the function you mentioned, and attempted to use it, but had the following error when compiling:
Code:
main.c: In function 'main':
main.c:64: error: 'labelZone1Temperature' undeclared (first use in this function)


Sorry if this is a dumb question. I'm fairly new to C, and completely new to GTK+. I just have to get used to it, I guess. Designing the interface has been a little challenging. MS just makes it so easy in Visual Studio, and I'm no Microsoft fan! :D

Here's the basic program I have so far. It's obviously in it's "prenatal" stages.lol You'll see where I called gtk_label_set_text towards the bottom.

Code:
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.c
* Copyright (C) Christopher Zeman
*
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <bonobo.h>
#include <gnome.h>
#include <glade/glade.h>

#include "callbacks.h"



void
some_handler(GtkWidget *widget)
{
    /* a handler referenced by the glade file.  Must not be static
     * so that it appears in the global symbol table. */
}


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

#ifdef ENABLE_NLS
   bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
   textdomain (GETTEXT_PACKAGE);
#endif

   gnome_program_init (PACKAGE, VERSION, LIBGNOMEUI_MODULE,
                      argc, argv,
                      GNOME_PARAM_APP_DATADIR, PACKAGE_DATA_DIR,
                      NULL);


   GladeXML *xml;
    GtkWidget *widget;

    gtk_init(&argc, &argv);
    xml = glade_xml_new("src/energy-management.glade", NULL, NULL);

    /* get a widget (useful if you want to change something) */
    /* widget = glade_xml_get_widget(xml, "widgetname"); */

    /* connect signal handlers */
    glade_xml_signal_autoconnect(xml);

   gtk_main ();
   
   gtk_label_set_text(GTK_LABEL(labelZone1Temperature), "Temp");
   return 0;
}


Thank you,
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 7:32 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
You never retrieve the label widget from the Glade file ... you have to use the following:

Code:
GtkWidget *labelZone1Temperature = glade_xml_get_widget (xml, "labelZone1Temperature");


The application knows nothing about that label in the code until you retrieve it.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 7:57 pm 
Offline
Familiar Face

Joined: Mon Oct 29, 2007 1:32 am
Posts: 8
Already then. It's working now. Thank you very much! I also had one other problem in the program. I had to call gtk_label_set_text immediately after I retrieved the widget.

Code:
GtkWidget *labelZone1Temperature = glade_xml_get_widget (xml, "labelZone1Temperature");
gtk_label_set_text(GTK_LABEL(labelZone1Temperature), "Test");


I will be updating these labels constantly in a DO WHILE loop. Do I need to retrieve the widget each time I update the label? I was just wondering since I had to move that line, or am I missing something here?

I really can't wait to get my copy of "Foundations of GTK+ Development". It shipped today, so I should have it soon.

Thanks again!
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 8:18 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
No, you don't. If you have a pointer to the widget, you can continue to use the pointer over and over. However, if you want to do this in another function, you will have to either pass the pointer to the function or retrieve the widget again. (I hope you enjoy the book!)

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 3:36 pm 
Offline
Familiar Face

Joined: Mon Oct 29, 2007 1:32 am
Posts: 8
I figured out where I went wrong. Hey, I learned something! lol

I called gtk_label_set_text() after gtk_main(). Upon further research, I found out about creating threads in GTK+ which is what I'll have to do in order to execute my loop.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 5:59 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Yes.

Code:
gtk_main ();
   
   gtk_label_set_text(GTK_LABEL(labelZone1Temperature), "Temp");


gtk_label_set_text() won't be called until AFTER you call gtk_main_quit(). Once you enter the main GTK+ loop, the application is in a sort of "event driven" mode. That is, you're waiting for an action from the user at which point a callback function is called. A user clicks a button and it's your callback function for that button is called (thing OnClick in visual basic)

In fact, you may want to read this: VB Programmer’s Intro to Linux Programming with GTK+

It sounds like you want to update your labels periodically, without user interaction. In that case, you may want to use a timeout function. If you read about the Main Event Loop in the GTK+ API, you'll see a function called g_timeout_add(). With this you can specify a function to call every x milliseconds. In this function, you can update your label.

For example:


Code:
gboolean
update_label (gpointer label)
{
    gtk_label_set_text(GTK_LABEL(label), "Updated");

    return FALSE: /* stop calling this function */
}

int
main (int argc, char *argv[])
{
    GladeXML *xml;
    GtkWidget *label, *window;

    gtk_init(&argc, &argv);

    xml = glade_xml_new ("src/energy-management.glade", NULL, NULL);

    label = glade_xml_get_widget (xml, "labelZone1Temperature");
    window = glade_xml_get_widget (xml, "window1");

    /* connect signal handlers */
    glade_xml_signal_autoconnect(xml);

   gtk_label_set_text(GTK_LABEL(label), "Initial Value");
   gtk_widget_show_all (window);

   /* set timout for 5 seconds */
   g_timeout_add(5000, (GSourceFunc)update_label, NULL);

   gtk_main ();

   return 0;
}


Please note... I just wrote that code off the top of my head. It probably doesn't quite work but hopefully it gives you the basic idea.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 6:09 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Actually, I just realized I had a demo of using g_timeout_add() to make a simple clock. Check it out: A GTK+ Clock Using g_timout_add

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 7:03 pm 
Offline
Familiar Face

Joined: Mon Oct 29, 2007 1:32 am
Posts: 8
Very cool! Thank you so much, Micah!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group