PyQT: PushButton receives commands while disabled
<p>I came across this problem and tried to break it down to the simplest code I could imagine: I created a GUI using Qt Designer V4.8.7 that only consists of a single pushButton with all default settings. It's called 'Test2.ui'. When the button is pressed, it's supposed to get disabled, print something in the terminal and afterwards gets enabled again. What happens is that I'm able to click on the disabled pushButton and it will repeat all the printing as many times as I clicked. This even works when I set the button invisible instead of disable it. I found similar problems on the internet, but none of the solutions seems to work for me â it's driving me mad. Anyone has an idea?</p>

<pre><code>from __future__ import division, print_function
import sys
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import QTimer
from time import sleep

qtCreatorFile = "Test2.ui" # Enter file here.

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class MyApp(QtGui.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.pushButton.pressed.connect(self.Test_Function)


    def Test_Function(self):
        self.pushButton.setEnabled(False)
        QtGui.QApplication.processEvents()
        print('Test 1')
        sleep(1)
        print('Test 2')
        sleep(1)
        self.pushButton.setEnabled(True)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())
</code></pre>

<p>Here is the code for 'Test2.ui'</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;ui version="4.0"&gt;
 &lt;class&gt;MainWindow&lt;/class&gt;
 &lt;widget class="QMainWindow" name="MainWindow"&gt;
  &lt;property name="geometry"&gt;
   &lt;rect&gt;
    &lt;x&gt;0&lt;/x&gt;
    &lt;y&gt;0&lt;/y&gt;
    &lt;width&gt;445&lt;/width&gt;
    &lt;height&gt;393&lt;/height&gt;
   &lt;/rect&gt;
  &lt;/property&gt;
  &lt;property name="mouseTracking"&gt;
   &lt;bool&gt;false&lt;/bool&gt;
  &lt;/property&gt;
  &lt;property name="focusPolicy"&gt;
   &lt;enum&gt;Qt::ClickFocus&lt;/enum&gt;
  &lt;/property&gt;
  &lt;property name="acceptDrops"&gt;
   &lt;bool&gt;false&lt;/bool&gt;
  &lt;/property&gt;
  &lt;property name="windowTitle"&gt;
   &lt;string&gt;MainWindow&lt;/string&gt;
  &lt;/property&gt;
  &lt;widget class="QWidget" name="centralwidget"&gt;
   &lt;widget class="QPushButton" name="pushButton"&gt;
    &lt;property name="enabled"&gt;
     &lt;bool&gt;true&lt;/bool&gt;
    &lt;/property&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;160&lt;/x&gt;
      &lt;y&gt;150&lt;/y&gt;
      &lt;width&gt;75&lt;/width&gt;
      &lt;height&gt;23&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="text"&gt;
     &lt;string&gt;Test&lt;/string&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
  &lt;/widget&gt;
  &lt;widget class="QMenuBar" name="menubar"&gt;
   &lt;property name="geometry"&gt;
    &lt;rect&gt;
     &lt;x&gt;0&lt;/x&gt;
     &lt;y&gt;0&lt;/y&gt;
     &lt;width&gt;445&lt;/width&gt;
     &lt;height&gt;22&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
  &lt;/widget&gt;
  &lt;widget class="QStatusBar" name="statusbar"/&gt;
 &lt;/widget&gt;
 &lt;resources/&gt;
 &lt;connections/&gt;
&lt;/ui&gt;
</code></pre>

<p>Your example code will not work because the test function blocks the gui. Whilst it is blocking, the button's disabled state is not updated properly, and so the <code>clicked</code> signal can still be emitted. The best way to avoid blocking the gui is to do the work in a separate thread:</p>

<pre><code>class Worker(QtCore.QObject):
    finished = QtCore.pyqtSignal()

    def run(self):
        print('Test 1')
        sleep(1)
        print('Test 2')
        sleep(1)
        self.finished.emit()

class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        ...

        self.pushButton.pressed.connect(self.handleButton)

        self.thread = QtCore.QThread(self)
        self.worker = Worker()
        self.worker.moveToThread(self.thread)
        self.worker.finished.connect(self.handleFinished)
        self.thread.started.connect(self.worker.run)

    def handleButton(self):
        self.pushButton.setEnabled(False)
        self.thread.start()

    def handleFinished(self):
        self.thread.quit()
        self.thread.wait()
        self.pushButton.setEnabled(True)
</code></pre>

python qt pyqt pyqt4 qpushbutton