[pygtk] PyGTK GUI update without signals from GUI

John Stowers john.stowers.lists at gmail.com
Tue Apr 15 05:26:12 WST 2008


On Sun, 2008-04-13 at 22:56 -0400, Andrew Lapidas wrote:
> Hi All:
> 
>  I am currently having a problem updating a GUI.
> 
>  I am using PyGTK and Glade to design an interface for a project.  This
>  interface contains no buttons, just images, labels, and a progress
>  bar.  The code for the project is small, and it basically does some
>  things independent of the GUI in an infinite loop.  I need the project
>  code to update the GUI intermittently.  I have found, though, that
>  generally it seems that the gtk.main loop is looking for signals from
>  the GUI and I cannot figure out how to give it signals from another
>  application.  I have thought spawning a new thread from the __init__
>  in the GUI and somehow having it send signals to gtk.main, but I do
>  not know if this will work.
> 
>  Any opinions or ideas on this subject would be greatly appreciated.

Programming in Pygtk is primarily event based, that is you respond to
signals sent from the gui (i.e. buttons clicked), do your task and
return. 

The mechanism by which signals are sent is the gtk main loop (i.e gtk
runs its own 'infinite loop' which iterates over all the sources of
events (like things being clicked), and dispatches these events to the
appropriate handler, and returns to looping (or not, if there are no
events to process). That is why you call gtk.main(), to start this loop.

Thus, if you want to do your own processing in your own infinite loop
you must remember that while your loop is looping, or blocking, the gtk
main loop cannot run, and so the gui will not be updated.

There are several ways to resolve this. 

You can structure your program to be event based, and execute your code
at regular intervals (check out the gobject.timeout_add functions which
get called every n seconds by the mainloop). You could also structure
your code to execute in response to user events, like button clicks.

You could also use threads to do you processing in. However, it is not
threadsafe to modify the gui (directly) from another thread. Check out
http://www.johnstowers.co.nz/blog/index.php/2007/03/12/threading-and-pygtk/ for patterns you can follow to allow easy threaded, worker based threaded programming in pygtk.

Finally, and not recommended, you could manually cause the main loop to
iterate (and hence update the gui) from your infinite loop. Remeber to
use threads_enter/leave and then cause an iteration by
gtk.main_iteration

John


> 
>  Thank you in advance,
>  Andy
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/



More information about the pygtk mailing list