GNOME applet using threads hangs
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)?
According to several comments on the gtk lists, you shouldn't be trying to update your user interface from threads. It would be better to poll the child threads from your main application. For references see here and here. More can be found by searching the archives. I don't know of any official documentation of this.
It might be too late for answering, but anyway hope this helps anyone jumped on this page.
http://faq.pygtk.org/index.py?file=faq20.006.htp&req=show
精彩评论