简体   繁体   English

如何在python中创建GUI的反应计时器?

[英]How do I create a reaction timer in python as GUI?

I have a problem with making a reaction timer. 我在设置反应计时器时遇到问题。 I want a button in my program that gives me the time it took to press the button from the time the timer started. 我想要程序中的一个按钮,该按钮为我提供了从计时器启动到按下按钮所花费的时间。

Let's say I open my program I want a button where if I click it, it will print the time it took me to press it after the timer started. 假设我打开程序我想要一个按钮,如果单击该按钮,它将显示计时器启动后我按下该按钮所花费的时间。 Then after I clicked the button I want the timer to reset and then again when I click the button it will print the time it took me to click the button again. 然后,在单击按钮后,我希望计时器重置,然后在单击按钮时再次显示,它将打印我再次单击按钮所花费的时间。

I already have the following code: 我已经有以下代码:

from tkinter import*
import time
import os
import datetime

s=0
m=0
h=0

def myClickMe1():
    myV=float(myValuta.get())
    Valuta=myV

    label3["text"]=Valuta*b
    label4["text"]=Valuta*c
    label5["text"]=Valuta*d
    label6["text"]=Valuta*e
    return

window=Tk()
myValuta=StringVar()

window.geometry("700x800")
window.title("Reaktionshastighehs test")

button1=Button(window, text="Klik her!", command=myClickMe1)

button1.place(x=330, y=460)

Just to be clear: I have already mate a button in the GUI but I want to make it work so that when I click it, it will print the time it took me to press it after the program started. 只是要清楚一点:我已经在GUI中匹配了一个按钮,但是我想使其工作,以便在单击该按钮时,它将打印程序启动后我按下该按钮所花费的时间。 And then if I press the button again it will tell me the time it took me to press it after I had pressed it the first time. 然后,如果我再次按下该按钮,它将告诉我第一次按下该按钮后花费的时间。

You can use the time library. 您可以使用time库。

import time
start = time.clock() # this is when your program starts
finish = time.clock() # this is after the user clicks the button

elapsed_time = finish - start # this is their 'reaction time'

Just call time.clock() whenever you need to know when the user did something (clicked first button or second button). 只要您需要知道用户何时进行操作(单击第一个按钮或第二个按钮),就只需调用time.clock() )。 Then you can simply use subtraction on these times. 然后,您可以简单地在这些时间使用减法。 The answer will be in seconds. 答案将以秒为单位。

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

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