简体   繁体   English

PyQt5 keyPressEvent的存储时间

[英]PyQt5 store time of keyPressEvent

Issue:问题:

I have a program where I will be showing several pages with a stacked widget, and users will have to press a button (using code I've written below) to go to the next page of the stacked widget.我有一个程序,我将在其中显示几个带有堆叠小部件的页面,用户必须按下一个按钮(使用我在下面编写的代码)到 go 到堆叠小部件的下一页。 What I am trying to do is to calculate the time the reaction time of the user.我要做的是计算用户的反应时间。 So to measure how quick they were to press a button.因此,要衡量他们按下按钮的速度。

Possible solution:可能的解决方案:

I was thinking of storing the time that a key was pressed (point B) and the time that the page was shown (point A) and to subtract these points to get the reaction time.我正在考虑存储按下键的时间(点 B)和显示页面的时间(点 A),并减去这些点以获得反应时间。 I was thinking of using a QTimer running throughout all the pages and whenever a key is pressed, it will store the time in a variable (that would be point b).我正在考虑使用在所有页面中运行的 QTimer,每当按下一个键时,它都会将时间存储在一个变量中(即 b 点)。 And store the time that the page was shown (point a), and subtract these points to get the reaction time.并存储页面显示的时间(点 a),减去这些点得到反应时间。 However, I am quite a beginner in coding and don't know how I would code this.但是,我是编码的初学者,不知道如何编码。 Or whether there is a simpler way.或者是否有更简单的方法。

The second solution I thought of was to used QElapsedTimer, but I don't know how to code that either.我想到的第二个解决方案是使用 QElapsedTimer,但我也不知道如何编写代码。

Button press:按钮按下:

I have also created a subclass of a QWidget that will emit a signal every time I press the key 'F' or 'J' (KeybaordWidget).我还创建了一个 QWidget 的子类,每次按下“F”或“J”键(KeybaordWidget)时都会发出一个信号。 So I was thinking, I would need to write a function that does what I have described and connect that function to the 'fPress' or 'jPress' signal.所以我在想,我需要编写一个 function 来完成我所描述的操作,并将 function 连接到“fPress”或“jPress”信号。

class KeyboardWidget (QWidget):
    fPress = pyqtSignal(str)
    jPress = pyqtSignal(str)
    def keyPressEvent(self,event):
        if event.key() == Qt.Key_F:
            self.fPress.emit('f')
        elif event.key() == Qt.Key_J:
            self.jPress.emit ('j')

In the keyPressEvent method, you can get the actual time in milliseconds since the epoch with time.time() , and save that in an attribute of your KeyboardWidget class.keyPressEvent方法中,您可以使用time.time() ,并将其保存在KeyboardWidget class 的属性中。 On the next keyPressEvent call, you can get the time again, subtract the first time from the second time, and you have the time in milliseconds the user needed between two button presses.在下一次keyPressEvent调用中,您可以再次获取时间,从第二次中减去第一次,您将获得用户在两次按钮按下之间所需的时间(以毫秒为单位)。

import time

class KeyboardWidget(QWidget):
    ...
    
    def __init__(self):
        QWidget.__init__(self)
        self.last_time = time.time()

    def keyPressEvent(self,event): 
        actual_time = time.time()
        print(actual_time - self.last_time) # will print the duration since the button was pressed the last time in milliseconds
        self.last_time = actual_time

        if event.key() == Qt.Key_F:
            self.fPress.emit('f')
        elif event.key() == Qt.Key_J:
            self.jPress.emit ('j')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM