PyGTK multiprocessing and updating GUI

I am trying to enable and disable the stop button to play sound in a GUI created using Glade PyGTK 2.0.

Basically, the program runs under control and an external process for reproducing sound.

I use multiprocessing (as threads are too slow) and I cannot turn off the stop button. I understand that this is due to the fact that processes cannot access the memory shared by the gtk widget thread.

Am I doing something wrong or is there a way to turn on the button after exiting the process?

#!/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: Nothing, I figured it out. I did not implement the threads correctly, and this caused a delay. Now it works great. Just 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() 
+6
source share

Source: https://habr.com/ru/post/927934/


All Articles