Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GUI not updated from another thread when using PyGtk

I am using PyGTK to build a GUI application. I want to update the textview widget from another thread but the widget is not getting updated everytime i try an update. What should i do to get a reliable GUI updating?

like image 467
appusajeev Avatar asked Jan 14 '10 19:01

appusajeev


2 Answers

GTK+ is not thread-safe, so you should not simply call GUI update methods from other threads. glib.idle_add (or gobject.idle_add in older PyGTK versions) can be used for this purpose.

Instead of writing:

label.set_text("foo")

you would write:

glib.idle_add(label.set_text, "foo")

which causes the function call to be queued in GTK+.

If you need to run multiple statements, it's often easier to wrap them in a function:

def idle():
    label1.set_text("foo")
    label2.set_text("bar")
glib.idle_add(idle)

Make sure that the function passed to idle_add does not return True; otherwise it will be queued again.

Edit: As Daniel pointed out, you need to call gtk.gdk.threads_init() anywhere in your program first.

like image 154
Johannes Sasongko Avatar answered Oct 24 '22 19:10

Johannes Sasongko


As stated in the previous answers, GTK is not "thread safe," but it is "thread-aware" - see this page on Threads: https://developer.gnome.org/gdk2/stable/gdk2-Threads.html

In order to modify GTK widgets from another thread you have to use GTK's locking. Call gtk.threads_init() immediately after importing the gtk module, and then you can update like so:

gtk.threads_enter()
# make changes...
gtk.threads_leave()

Note that the above will not work on Windows (see the link above). On Windows you must use gobject.idle_add() as explained above, though don't forget to put gobject.threads_init() directly after importing gobject in your code! The idle_add() function will execute the update itself in the main thread (the thread running gtk.main()).

like image 2
Daniel G Avatar answered Oct 24 '22 17:10

Daniel G