I am trying to develop a GNOME applet (put into panel) using python (pyGTK). I've started by following the tutorial suggested in other SO question.
My plan is to let the applet do something in the background in a repetitive fashion (causing its display to be updated). So I thought I am gonna need threads to do it. I've seen several tutorials on how to use threads with pyGTK - most of them follow the pyGTK FAQ. And all of them suggest being cautious.
I tried with the different versions, incl.
#!/usr/bin/python
import pygtk
import sys
pygtk.require('2.0')
import gtk
import gobject
gobject.threads_init()
import gnomeapplet
import time
from threading import Thread
def threadFunction(label):
gobject.idle_add(label.set_text, 'In the thread')
def factory(applet, iid):
text = gtk.Label('Start %s' % iid)
applet.add(text)
applet.show_all()
Thread(target=threadFunction, args=(text)).start()
return True
if __name__ == '__main__':
print "Starting factory"
gnomeapplet.bonobo_factory("OAFIID:Gnome_Panel_Example_Factory", gnomeapplet.Applet.__gtype__, "Simple gnome applet example", "1.0", factory)
But it doesn't work. The thread execution seems to hang when trying to update the presentation (gobject.idle_add
). I tried:
- replacing
gobject.threads_init()
withgtk.gdk.threads_init()
- because this is what some of the tutorials use, - subclassing threading.Thread class instead of using
Thread(target=)
- using
gtk.threads_enter
andgtk.threads_leave
around any code that is run within a separate thread and updates the widgets,
What is my mistake then?
Is threading imcompatible with applets (as opposed to other pyGTK programs)?