Re: displaying continuosly in entry widget



"shibu alampatta" wrote:

On pressing a button i wanted a list of text to be displayed on entry
widget, on after the other ( in a for loop), with some delay, say
sleep(3). but the problem is  the last text only getting visible. if i
increase the sleep argument then also the same. any help.........?

In short:
- NEVER use sleep() or similar in GTK+ apps
- make all your handlers return as quickly as possible to GTK+
- use a timeout handler for periodic events, such as text changes

In long:
sleep() (or nanosleep() or similar) MUST NOT be used in any GUI program!
They are of use for old style console or daemon programs only. There,
your application usually is in full control of execution, scheduling of
events, delays and even the entire (text) screen. GUI programs work
fundamentally different.

In a GUI application (a GTK+ application in our case) it is GTK+ that
has full control over your application. You hand control over to GTK+
when entering gtk_main() loop. From that point on, execution of your
application is basically limited to particular functions (handlers),
which are called by GTK+. It's basically up to GTK+ when and in what
order they are called.

Your handlers may be arbitrarily complex. They my call numerous other
functions, your own ones or GTK+ ones. Handlers for buttons or menu
entries may even open new windows or dialogs. However, all handlers have
one thing in common: they are supposed to return control to GTK+ as
quickly as possible. Especially without artificial delays (like sleep ()
or similar)!

Returning control to GTK+ means either finishing a handler function that
has been called as quickly as possible, or for instance calling the GTK+
function to open modal dialogs.

Graphical updates, i.e. of your text, can take place only while GTK+ is
in control of program execution. The standard widgets get updated /
redrawn automatically when your handler function returns to GTK+. While
sleep()ing, there is no return to GTK+, but the whole process is put
asleep instead. Since it's the process itself who is responsible for
redrawing the widgets, there will be no redraws (and no other sign of
life either) while being asleep.

If you want to let your application carry out tasks on a periodic basis,
i.e. changing text of a GtkEntry, you have to do it another way. You
have to inform GTK+ that you want to do something every i.e. 3 seconds.
You do this by calling gtk_timeout_add() or g_timeout_add(). It gets a
function you provide as an argument. It informs GTK+ that this function
wants to be called every 3 seconds. Then, if GTK+ is in control of
program execution as it is supposed to be, it will call this function
every 3 seconds, indeed.

This function of yours should do nothing more than changing the text of
the GtkEntry in the desired way (probably just one call to
gtk_entry_set_text() ) and return to GTK+. As soon as control is
returned to GTK+, the widget will be redrawn. When you don't need the
periodic update any more, you just destroy your timeout handler by one
of the described means.

See:
http://developer.gnome.org/doc/API/2.0/gtk/gtk-General.html#gtk-timeout-add
http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html#g-timeout-add



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]