PyGTK Multiprocessing and Updating GUI [closed]

橙三吉。 提交于 2019-12-23 09:36:29

问题


I am trying to enable and disable a stop button for audio playback in a GUI created using Glade PyGTK 2.0.

The program basically works by running and external process to play the audio.

I am using multiprocessing(as threading is too slow) and I can't get the stop button to disable. I understand this is due to processes not being able to access the memory shared by the gtk widget thread.

Am I doing something wrong, or is there any way to enable the button once the process exits?

#!/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()

EDIT: Never mind, I figured it out. I had not implemented threading properly and that caused the lag. It works fine now. Just have to change the FooBar method to:

    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()

来源:https://stackoverflow.com/questions/12933970/pygtk-multiprocessing-and-updating-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!