简体   繁体   中英

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. 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.

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). Then you can simply use subtraction on these times. The answer will be in seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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