1

Please take a look on my test code below:

import time
import subprocess
import os,re
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MyWindow(Gtk.Window):
   def __init__(self):
      Gtk.Window.__init__(self, title="test")
      self.set_default_size(400, 190)
      self.set_border_width(10)
      fixed = Gtk.Fixed()
      self.add(fixed)

      self.button2 = Gtk.Button(label='press')
      self.button2.connect("clicked", self.aaabbb)
      self.button2.set_size_request(100, 20)
      fixed.put(self.button2, 170, 10)

   def aaabbb(self,widget):
      self.button2.set_sensitive(False)
      p = subprocess.run( ['/bin/bash','-c', '/bin/sleep 5'] )
      print(p.returncode)

window = MyWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

If you run this simple application you will notice that button gets disabled only after 5 second, after subprocess finishes "sleep 5"

Question - how to make the button insensitive before subprocess starts?

Thank you in advance

José Fonte
  • 4,016
  • 2
  • 16
  • 28

1 Answers1

1

That happens because the subprocess call isn't asynchronous and blocks the mainloop and the user interface as a consequence.

So, the approach is not correct.

The clicked signal requires a button to be pressed and released but you have other signals that you can use, eg. button-press-event and button-release-event. The callback prototypes vary from the clicked one and you'll have access to the event, if needed.

The answer to your question would be, set a callback to the button press event, BUT this will not solve your problem! The call should be async and to achieve that you can use GLib spawn async method.

José Fonte
  • 4,016
  • 2
  • 16
  • 28
  • Can I use "multiprocessing" – user1078796 Dec 12 '17 at 19:59
  • If you don't call gtk methods from the subprocesses probably there won't be any problem. The stress will arise when you want to communicate with the mainloop. Pipes are problematic: "Note that data in a pipe may become corrupted if two processes (or threads) try to read from or write to the same end of the pipe at the same time. ", Queues seem to be "thread and process safe" and don't forget they must be asynchronous calls . Anyway, i would suggest that you isolate problems. Unless you have a specific reason to use multiprocessing, try GLib trhreads/thread pools and async queues. – José Fonte Dec 12 '17 at 21:12