Python PyGTK多处理和更新GUI

Python PyGTK多处理和更新GUI,python,multithreading,gtk,pygtk,multiprocessing,Python,Multithreading,Gtk,Pygtk,Multiprocessing,我试图在使用Glade PyGTK 2.0创建的GUI中启用和禁用音频播放的停止按钮 该程序基本上通过运行和外部进程来播放音频 我正在使用多处理(因为线程太慢),无法禁用停止按钮。我理解这是由于进程无法访问gtk小部件线程共享的内存 我是否做错了什么,或者是否有任何方法可以在流程退出后启用该按钮 #!/usr/bin/python import pygtk import multiprocessing import gobject from subprocess import Popen, PI

我试图在使用Glade PyGTK 2.0创建的GUI中启用和禁用音频播放的停止按钮

该程序基本上通过运行和外部进程来播放音频

我正在使用多处理(因为线程太慢),无法禁用停止按钮。我理解这是由于进程无法访问gtk小部件线程共享的内存

我是否做错了什么,或者是否有任何方法可以在流程退出后启用该按钮

#!/usr/bin/python
import pygtk
import multiprocessing
import gobject
from subprocess import Popen, PIPE
pygtk.require("2.0")
import gtk
import threading

gtk.threads_init()

class Foo:
    def __init__(self): 
        #Load Glade file and initialize stuff

    def FooBar(self,widget):
        self.stopButton.set_sensitive(True)#make the Stop button visible in the user section
        def startProgram():
            #run program
            gtk.threads_enter()
            try:
                self.stopButton.set_sensitive(False) 
            finally:
                gtk.threads_leave()
            print "Should be done now"

        thread = multiprocessing.Process(target=startProgram)
        thread.start()

if __name__ == "__main__":
prog = Foo()
gtk.threads_enter()
gtk.main()
gtk.threads_leave()
编辑:没关系,我想出来了。我没有正确地实现线程,这导致了延迟。现在很好用。只需将FooBar方法更改为:

    def FooBar(self,widget):
        self.stopButton.set_sensitive(True)#make the Stop button visible in the user section
        def startProgram():
            #run program
            Popen.wait() #wait until process has terminated
            gtk.threads_enter()
            try:
                self.stopButton.set_sensitive(False) 
            finally:
                gtk.threads_leave()
            print "Should be done now"

        thread = threading.Thread(target=startProgram)
        thread.start()

线程是如何执行的?它是否到达
打印
-语句?如果是这样,当调用
set\u sensitive(False)
时(如果它不在
try
范围内),如果有回溯,回溯会是什么样子?线程确实到达了print语句。在try-catch块外设置set-sensitive(False)不会改变任何东西(即不做任何事情)另一项检查:您对
set-sensitive(True)
的注释说“使…可见”,但
set-sensitive
不会改变可见性(
hide
/
show
确实如此)。。。所以这并不是说:按钮一直都是可见的,但它只是你想切换的灵敏度?是的,我想切换灵敏度,而不是可见性,即启用/禁用按钮,而不是隐藏/显示它。它在startProgram函数之外工作正常。我是否可以等待创建的进程退出,而不冻结GUI?您应该能够使用
gobject
(例如,使用)创建回调函数,并将
线程
传递给它。然后让回调函数通过
thread.is\u alive()
(如果不只是设置一个新的回调函数)检查线程是否仍然处于活动状态。