PyGTK多处理和更新GUI

mis*_*Man 6 python gtk multithreading pygtk multiprocessing

我正在尝试在使用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()
Run Code Online (Sandbox Code Playgroud)

编辑:没关系,我想通了.我没有正确实现线程,这导致了滞后.它现在工作正常.只需将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()
Run Code Online (Sandbox Code Playgroud)