我正在尝试使用 python (pyGTK) 开发一个 GNOME 小程序(放入面板)。我已经按照其他 SO question 中建议的教程开始。
我的计划是让小程序以重复的方式在后台执行某些操作(导致其显示被更新)。所以我想我需要线程来做到这一点。我看过几个关于如何在 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()
- 因为这是一些教程使用的, - 子类化 threading.Thread 类而不是使用
Thread(target=)
- 使用
gtk.threads_enter
和gtk.threads_leave
围绕在单独线程中运行并更新小部件的任何代码,
那我的错误是什么?
线程是否与小程序不兼容(与其他 pyGTK 程序相反)?