问题
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