使用线程的 GNOME 小程序挂起
我正在尝试使用 python (pyGTK) 开发一个 GNOME 小程序(放入面板中)。我首先遵循 其他SO问题中建议的教程。
我的计划是让小程序在后台以重复的方式执行某些操作(导致其显示更新)。所以我想我需要线程来完成它。我看过几个关于如何在 pyGTK 中使用线程的教程 - 大多数都遵循 pyGTK 常见问题解答。所有这些都表明要谨慎。
我尝试了不同的版本,包括。
#!/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
)。我尝试:
- 用
gtk.gdk.threads_init()
替换gobject.threads_init()
- 因为这是一些教程使用的, - 子类化 threading.Thread 类而不是使用
Thread(target=)
- 使用
gtk.threads_enter
和gtk.threads_leave
围绕在单独线程中运行并更新小部件的任何代码,
< strong>那我的错误是什么?
线程与小程序不兼容(与其他 pyGTK 程序相反)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 gtk 列表上的几条评论,您不应该尝试从线程更新用户界面。最好从主应用程序轮询子线程。有关参考,请参阅此处和此处。通过搜索档案可以找到更多信息。我不知道这方面有任何官方文件。
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.
现在回答可能为时已晚,但无论如何希望这可以帮助任何跳到此页面的人。
http://faq.pygtk.org/index.py ?file=faq20.006.htp&req=show
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