[PyQt] discard button presses while some function is running

Samuele Carcagno sam.carcagno at gmail.com
Tue Mar 15 09:47:02 GMT 2011


Hi list users,

I'm building a program to perform some auditory tests.
The user is presented with some sounds and then has to
press a button on the numeric keypad depending on which
sound was presented. I would like that button presses
given while the sounds are being presented were ignored,
and only button presses given after the sounds have finished
playing were accepted. I've been unable to achieve this,
two simplified examples are attached. Clicking on the button
starts a trial (in the actual program this starts the presentation
of the sounds, here it just waits for 3 seconds). Keyboard responses
given during the trial seem to be queued up and processed at the end
of the trial. Even if I set a flag, as in Example 2, 
to process the key presses only if self.trialRunning is False, this
doesn't seem to work, the keypresses seem to be queued up and processed
after the trial.

Thanks for any help.

Sam


##Example 1 ========================
import sys, time
from PyQt4 import QtGui
from PyQt4 import QtCore

class Example(QtGui.QWidget):
  
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Checkbox')

        self.button1 = QtGui.QPushButton('Button 1', self)
        QtCore.QObject.connect(self.button1,
                               QtCore.SIGNAL('clicked()'), self.onClickButton1)

    def onClickButton1(self):
        self.doTrial()

    def doTrial(self):
        print('Trial running') #this is where the sounds are presented
        time.sleep(3)

    def keyPressEvent(self, event):
        if (event.type() == QtCore.QEvent.KeyPress):
            if event.key()==QtCore.Qt.Key_0:
                buttonClicked = 0
            elif event.key()==QtCore.Qt.Key_1:
                buttonClicked = 1
            elif event.key()==QtCore.Qt.Key_2:
                buttonClicked = 2
            elif event.key()==QtCore.Qt.Key_3:
                buttonClicked = 3
            else:
                buttonClicked = 0
            print(buttonClicked)
            

if __name__ == '__main__':
  
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()
#*****************************************


#Example 2 ===============================
import sys, time
from PyQt4 import QtGui
from PyQt4 import QtCore

class Example(QtGui.QWidget):
  
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(300, 300, 250, 150)
        self.trialRunning = False
        self.button1 = QtGui.QPushButton('Button 1', self)
        QtCore.QObject.connect(self.button1,
                               QtCore.SIGNAL('clicked()'), self.onClickButton1)

    def onClickButton1(self):
        self.doTrial()

    def doTrial(self):
        self.trialRunning = True
        print('Trial running') #this is where the sounds are presented
        time.sleep(3)
        self.trialRunning = False

    def keyPressEvent(self, event):
        if (event.type() == QtCore.QEvent.KeyPress) and self.trialRunning == False:
            if event.key()==QtCore.Qt.Key_0:
                buttonClicked = 0
            elif event.key()==QtCore.Qt.Key_1:
                buttonClicked = 1
            elif event.key()==QtCore.Qt.Key_2:
                buttonClicked = 2
            elif event.key()==QtCore.Qt.Key_3:
                buttonClicked = 3
            else:
                buttonClicked = 0
            print(buttonClicked)
            

if __name__ == '__main__':
  
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()

#*****************************************


More information about the PyQt mailing list