user1078796
user1078796

Reputation: 15

Python3 GTK3 - Change button to insensitive immediately after click

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

Upvotes: 1

Views: 400

Answers (1)

José Fonte
José Fonte

Reputation: 4104

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.

Upvotes: 1

Related Questions