使用线程的GNOME applet挂起

时间:2010-01-12 23:27:29

标签: python multithreading gtk pygtk gnome

我正在尝试使用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() - 因为这是一些教程使用的内容,
  • 继承threading.Thread类而不是使用Thread(target=)
  • 在单独的线程中运行的任何代码周围使用gtk.threads_entergtk.threads_leave并更新小部件,

那么我的错误是什么?

线程是否与applet不兼容(与其他pyGTK程序相反)?

2 个答案:

答案 0 :(得分:2)

根据gtk列表上的几条评论,您不应该尝试从线程更新用户界面。最好从主应用程序中轮询子线程。有关参考资料,请参阅herehere。通过搜索档案可以找到更多内容。我不知道这方面的任何官方文件。

答案 1 :(得分:0)

回答可能为时已晚,但无论如何希望这有助于任何人跳过这个页面。

http://faq.pygtk.org/index.py?file=faq20.006.htp&req=show

相关问题