Python/Tkinker: Why does my reaction test always return a reaction time of 0 without me pressing anything?

I'm always told "Reaction time: 0" without pressing anything. Does anyone know why this is?

 from tkinter import * import random window = Tk() window.title("Reaktionstest") window.geometry("800x800") window.configure(bg="white") limit = 0 score = random.randint(9, 10) reaction_time = 0 green = False def verzögerung():  global score, limit, green  score -= 1  if score = limit:    # schedule next update 1 second later    window.after(1000, reaktion)  else:    green = False # Setze green auf False, wenn score kleiner oder gleich limit ist    show_end_screen() def reaktion():  global green, reaction_time  if green:    reaction_time += 1    if rB.winfo_exists(): # Überprüfung, ob der Button noch existiert      rB.configure(bg="red")    print("Red")  else:    green = True    if rB.winfo_exists(): # Überprüfung, ob der Button noch existiert      rB.configure(bg="green")    print("Green") def show_end_screen():  global reaction_time # Zugriff auf die globale Variable reaction_time  rB.destroy()  reaction_time_label = Label(window, text=f"Reaktionszeit: {reaction_time} Sekunden", bg="white", fg="black", font=("Arial", 24))  reaction_time_label.place(x=250, y=150) ueberschrift = Label(window, text="Reaktionstest", bg="lightcyan", fg="black", font=("Arial", 44, "bold")) ueberschrift.place(x=234, y=50) rB = Button(window, bg="red", text="Drücke mich, wenn ich grün werde", font=("Arial", 10), command=reaktion) rB.place(x=100, y=100, width=400, height=400) limit = score # Setze limit auf den Wert von score window.after(1000, verzögerung) # start the update 1 second later window.mainloop()
(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
3 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
1 year ago

Within the function deceleration you have a syntax error in if– Head.

if score = limit:

Probably you want to compare both variables with each other (otherwise, the previous action would make no sense). For this you need the comparison operator (==).

Since both variables cannot be equal to value at that time (the variable score is always 1 smaller than limit), the other– Run through the hull.

In show_end_screen the reaction time is output. Since she hasn’t changed until then, she’s 0.

regex9
1 year ago
Reply to  Diamondblow

The variablescoreas I said always 1 smaller thanlimit. In the lower part of the program limit to the same value, in deceleration you are score 1.