Basically, I'm trying to make a small game of sorts where the user has to click a button as many times as they can before the time runs out (5 seconds).
After 5 seconds have passed, the button is greyed/disabled. However, my code has trouble working, because the 'timer' works only when the user clicks the button. I want the timer to run regardless of whether the user has clicked the button.
So basically, when the program is run, even if the user hasn't clicked the button and 5 seconds have passed, the button should be greyed/disabled.
Here is my code:
from tkinter import *
import time
class GUI(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.result = 0
self.grid()
self.buttonClicks = 0
self.create_widgets()
def countTime(self):
self.end = time.time()
self.result =self.end - self.start
return (self.result)
def create_widgets(self):
self.start = time.time()
self.button1 = Button(self)
self.label = Label(self, text=str(round(self.countTime(),1)))
self.label.grid()
self.button1["text"] = "Total clicks: 0"
self.button1["command"] = self.update_count
self.button1.grid()
def update_count(self):
if(self.countTime() >=5):
self.button1.configure(state=DISABLED, background='cadetblue')
else:
self.buttonClicks+=1
self.button1["text"] = "Total clicks: " + str(self.buttonClicks)
root = Tk()
root.title("Something")
root.geometry("300x300")
app = GUI(root)
root.mainloop()