我正在尝试使用python(pyGTK)开发一个GNOME applet(放入面板)。我开始关注tutorial suggested in other SO question。
我的计划是让applet以重复的方式在后台执行某些操作(导致其显示更新)。所以我以为我需要线程去做。我已经看过几个关于如何在pyGTK中使用线程的教程 - 其中大部分都遵循pyGTK FAQ。所有这些都暗示着谨慎。
我尝试了不同的版本,包括
#!/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)
但它不起作用。尝试更新演示文稿时,线程执行似乎挂起(gobject.idle_add
)。我试过了:
gobject.threads_init()
替换为gtk.gdk.threads_init()
- 因为这是一些教程使用的内容,Thread(target=)
gtk.threads_enter
和gtk.threads_leave
并更新小部件,那么我的错误是什么?
线程是否与applet不兼容(与其他pyGTK程序相反)?
答案 0 :(得分:2)
答案 1 :(得分:0)
回答可能为时已晚,但无论如何希望这有助于任何人跳过这个页面。