使用线程的 GNOME 小程序挂起

发布于 2024-08-17 19:38:57 字数 1560 浏览 4 评论 0原文

我正在尝试使用 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_entergtk.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() with gtk.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 and gtk.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷港 2024-08-24 19:38:57

根据 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.

天荒地未老 2024-08-24 19:38:57

现在回答可能为时已晚,但无论如何希望这可以帮助任何跳到此页面的人。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文