python - 使用线程的GNOME小程序挂起

标签 python multithreading gtk pygtk gnome

我正在尝试使用python(pyGTK)开发GNOME小程序(放入面板)。我首先关注tutorial suggested in other 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_entergtk.threads_leave并更新小部件

  • 那我的错误是什么?

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

    最佳答案

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

    关于python - 使用线程的GNOME小程序挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2053241/

    相关文章:

    python - 交错两个 numpy 索引数组,每个数组中的一项

    python - BeautifulSoup - 类型错误 : sequence item 0: expected str instance

    C pthread_create 和 join 在 main 函数之外

    c# - 如何计算.NET 应用程序中的并发线程数?

    python - 为什么gtk.Dialog在没有获得焦点时直接消失

    python - 遍历字符串列表,从每个字符串项中删除所有禁用词

    c++ - 使用 pthread 的简单程序的输出

    c - 如何在用C语言的Glade/GTK制作的窗口中放置数字时钟?

    c - 从 GtkListStore 检索数据

    python - 如何使用 np.array() 和 np.arange(3) 的组合手动创建一个三乘三乘三的 NumPy 数组?