I am trying to build a CPS-Test app with Python Tkinter. I have been trying to make a button that when clicked, increases your clicks (data["count"]
). It takes your clicks in 10 seconds and calculates the speed. Then it destroys the window and prints the speed.
I will soon remove the print thing but for now I want to keep it while the problem is still there. However, what is happening is my window is not rendering until 10 seconds is up, so as soon as you click the button, it just ends and the speed is 0.1 cps.
I don't understand why this is happening, as no errors are popping up. I tried modifying the code so the Button
is rendered first, which made the page show, but then an error pops up saying that the Button
command is not defined until after the Button
is made.
I tried using the .pack
method after making the button and keeping them separate, but it didn't work and ended up doing the same thing. Why is this happening?
from tkinter import *
from time import sleep
tab = Tk()
tab.geometry('500x500')
tab.resizable(0, 0)
tab.title("")
data = {"count": 0, "time": 0}
def click():
data["count"] += 1
if data["time"] == 10:
cps = data["count"]/data["time"]
Label(tab, text = str(cps) + " clicks per second!", font = "helvetica 30 bold underline italic", padx = 50, pady = 50).pack()
print(cps)
tab.destroy()
quit()
btn = Button(tab, text = 'Click Me!', font = "helvetica 24 bold", command = click, background = "blue", activebackground = "yellow").pack()
def countTime():
sleep(1)
data["time"] += 1
for i in range(0, 10):
countTime()