Windows 上线程和 PyGTK 的执行顺序

发布于 2024-07-24 13:19:28 字数 1291 浏览 4 评论 0原文

我在 Windows 上遇到线程和 PyGTK 问题。 根据 PyGTK FAQ (以及我的自己的实验),从子线程可靠地更新 GUI 的唯一方法是使用 gobject.idle_add 函数。 但是,无法保证何时调用该函数。 如何保证 gobject.idle_add 后面的行在它指向的函数之后被调用?

非常简单且人为的示例:

import gtk
import gobject
from threading import Thread
class Gui(object): def __init__(self): self.button = gtk.Button("Click") self.button.connect("clicked", self.onButtonClicked) self.textEntry = gtk.Entry() self.content = gtk.HBox() self.content.pack_start(self.button) self.content.pack_start(self.textEntry) self.window = gtk.Window() self.window.connect("destroy", self.quit) self.window.add(self.content) self.window.show_all()
def onButtonClicked(self, button): Thread(target=self.startThread).start()
def startThread(self): #I want these next 2 lines to run in order gobject.idle_add(self.updateText) print self.textEntry.get_text()
def updateText(self): self.textEntry.set_text("Hello!")
def quit(self, widget): gtk.main_quit()

gobject.threads_init() x = 桂() gtk.main()

I'm having issues with threads and PyGTK on Windows. According the the PyGTK FAQ (and my own experimentation), the only way to reliably update the GUI from a child thread is to use the gobject.idle_add function. However, it can't be guaranteed when this function will be called. How can I guarantee that the line following the gobject.idle_add gets called after the function it points to?

Very simple and contrived example:

import gtk
import gobject
from threading import Thread
class Gui(object): def __init__(self): self.button = gtk.Button("Click") self.button.connect("clicked", self.onButtonClicked) self.textEntry = gtk.Entry() self.content = gtk.HBox() self.content.pack_start(self.button) self.content.pack_start(self.textEntry) self.window = gtk.Window() self.window.connect("destroy", self.quit) self.window.add(self.content) self.window.show_all()
def onButtonClicked(self, button): Thread(target=self.startThread).start()
def startThread(self): #I want these next 2 lines to run in order gobject.idle_add(self.updateText) print self.textEntry.get_text()
def updateText(self): self.textEntry.set_text("Hello!")
def quit(self, widget): gtk.main_quit()

gobject.threads_init() x = Gui() gtk.main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

计㈡愣 2024-07-31 13:19:28

不要尝试从线程更新或访问 GUI。 你只是自找麻烦。 例如,“get_text”在线程中完全工作几乎是一个意外。 您也许可以在 GTK 中依赖它 - 尽管我什至不确定 - 但您无法在其他 GUI 工具包中这样做。

如果您确实需要在线程中执行某些操作,则应该在启动线程之前从 GUI 获取所需的数据,然后使用 idle_add,就像这样:

import time
import gtk
import gobject
from threading import Thread

w = gtk.Window()
h = gtk.HBox()
v = gtk.VBox()
addend1 = gtk.Entry()
h.add(addend1)
h.add(gtk.Label(" + "))
addend2 = gtk.Entry()
h.add(addend2)
h.add(gtk.Label(" = "))
summation = gtk.Entry()
summation.set_text("?")
summation.set_editable(False)
h.add(summation)
v.add(h)
progress = gtk.ProgressBar()
v.add(progress)
b = gtk.Button("Do It")
v.add(b)
w.add(v)
status = gtk.Statusbar()
v.add(status)
w.show_all()

def hardWork(a1, a2):
    messages = ["Doing the hard work to add %s to %s..." % (a1, a2),
                "Oof, I'm working so hard...",
                "Almost done..."]
    for index, message in enumerate(messages):
        fraction = index / float(len(messages))
        gobject.idle_add(progress.set_fraction, fraction)
        gobject.idle_add(status.push, 4321, message)
        time.sleep(1)
    result = a1 + a2
    gobject.idle_add(summation.set_text, str(result))
    gobject.idle_add(status.push, 4321, "Done!")
    gobject.idle_add(progress.set_fraction, 1.0)


def addthem(*ignored):
    a1 = int(addend1.get_text())
    a2 = int(addend2.get_text())
    Thread(target=lambda : hardWork(a1, a2)).start()

b.connect("clicked", addthem)
gtk.gdk.threads_init()
gtk.main()

如果你真的、绝对需要在线程中间从 GUI 读取数据(这是一个非常糟糕的主意,不要这样做 - 你可能会陷入真正令人惊讶的死锁,尤其是当程序运行时)关闭)Twisted 中有一个实用程序, blockingCallFromThread< /a>,这将为您完成艰苦的工作。 你可以这样使用它:

from twisted.internet.gtk2reactor import install
install()
from twisted.internet import reactor

from twisted.internet.threads import blockingCallFromThread
from threading import Thread

import gtk

w = gtk.Window()
v = gtk.VBox()
e = gtk.Entry()
b = gtk.Button("Get Text")

v.add(e)
v.add(b)
w.add(v)

def inThread():
    print 'Getting value'
    textValue = blockingCallFromThread(reactor, e.get_text)
    print 'Got it!', repr(textValue)

def kickOffThread(*ignored):
    Thread(target=inThread).start()

b.connect("clicked", kickOffThread)
w.show_all()

reactor.run()

如果你想看看魔法是如何工作的,你可以随时 阅读源代码

Don't try to update or access your GUI from a thread. You're just asking for trouble. For example, the fact that "get_text" works at all in a thread is almost an accident. You might be able to rely on it in GTK - although I'm not even sure about that - but you won't be able to do so in other GUI toolkits.

If you have things that really need doing in threads, you should get the data you need from the GUI before launching the thread, and then update the GUI from the thread by using idle_add, like this:

import time
import gtk
import gobject
from threading import Thread

w = gtk.Window()
h = gtk.HBox()
v = gtk.VBox()
addend1 = gtk.Entry()
h.add(addend1)
h.add(gtk.Label(" + "))
addend2 = gtk.Entry()
h.add(addend2)
h.add(gtk.Label(" = "))
summation = gtk.Entry()
summation.set_text("?")
summation.set_editable(False)
h.add(summation)
v.add(h)
progress = gtk.ProgressBar()
v.add(progress)
b = gtk.Button("Do It")
v.add(b)
w.add(v)
status = gtk.Statusbar()
v.add(status)
w.show_all()

def hardWork(a1, a2):
    messages = ["Doing the hard work to add %s to %s..." % (a1, a2),
                "Oof, I'm working so hard...",
                "Almost done..."]
    for index, message in enumerate(messages):
        fraction = index / float(len(messages))
        gobject.idle_add(progress.set_fraction, fraction)
        gobject.idle_add(status.push, 4321, message)
        time.sleep(1)
    result = a1 + a2
    gobject.idle_add(summation.set_text, str(result))
    gobject.idle_add(status.push, 4321, "Done!")
    gobject.idle_add(progress.set_fraction, 1.0)


def addthem(*ignored):
    a1 = int(addend1.get_text())
    a2 = int(addend2.get_text())
    Thread(target=lambda : hardWork(a1, a2)).start()

b.connect("clicked", addthem)
gtk.gdk.threads_init()
gtk.main()

If you really, absolutely need to read data from the GUI in the middle of a thread (this is a really bad idea, don't do it - you can get into really surprising deadlocks, especially when the program is shutting down) there is a utility in Twisted, blockingCallFromThread, which will do the hard work for you. You can use it like this:

from twisted.internet.gtk2reactor import install
install()
from twisted.internet import reactor

from twisted.internet.threads import blockingCallFromThread
from threading import Thread

import gtk

w = gtk.Window()
v = gtk.VBox()
e = gtk.Entry()
b = gtk.Button("Get Text")

v.add(e)
v.add(b)
w.add(v)

def inThread():
    print 'Getting value'
    textValue = blockingCallFromThread(reactor, e.get_text)
    print 'Got it!', repr(textValue)

def kickOffThread(*ignored):
    Thread(target=inThread).start()

b.connect("clicked", kickOffThread)
w.show_all()

reactor.run()

If you want to see how the magic works, you can always read the source.

ぃ弥猫深巷。 2024-07-31 13:19:28

您可以将这两个函数包装到另一个函数中,并在此函数上调用idle_add:

def update_and_print(self):
        self.updateText()
        print self.textEntry.get_text()

def startThread(self):
        gobject.idle_add(self.update_and_print)

You could wrap the two functions into another function and call idle_add on this function:

def update_and_print(self):
        self.updateText()
        print self.textEntry.get_text()

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