代码之家  ›  专栏  ›  技术社区  ›  Grzegorz Oledzki

使用线程挂起的GNOME小程序

  •  2
  • Grzegorz Oledzki  · 技术社区  · 15 年前

    我正在尝试使用python(pyGTK)开发一个GNOME小程序(放入面板)。我从以下几点开始 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() -因为这是一些教程使用的,
    • 子类化线程。线程类,而不是使用 Thread(target=)
    • gtk.threads_enter gtk.threads_leave 围绕在单独线程中运行并更新小部件的任何代码,

    那我犯了什么错?

    线程是否与小程序兼容(相对于其他pyGTK程序)?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Matthew Talbert    15 年前

    根据gtk列表上的一些评论,您不应该试图从线程更新用户界面。最好从主应用程序轮询子线程。有关参考资料,请参阅 here here

        2
  •  0
  •   The_Cute_Hedgehog    11 年前

    现在回答可能已经太晚了,但无论如何,希望这能帮助任何一个登上这一页的人。

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