0

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()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

0

You should be running a different thread (As seen here: Tkinter: How to use threads to preventing main event loop from "freezing") to be running the timer.

Check this other question to get an idea on how your other thread should be like How to create a timer using tkinter?

Community
  • 1
  • 1
  • this problem definitely doesn't need threads. Threads add considerable complexity, and in this case provide absolutely no value. – Bryan Oakley Apr 12 '17 at 11:02
0

You must create a timer using the after()

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()
        self.isRunning = True
        self.update_clock()
        self.master = master


    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.isRunning:
            self.buttonClicks+=1
            self.button1["text"] = "Total clicks: " + str(self.buttonClicks)

    def update_clock(self):
        t = round(self.countTime(), 1)
        self.label.configure(text=str(t))
        if t < 5:
            self.master.after(100, self.update_clock)
        else:
            self.isRunning = False


root = Tk()
root.title("Something")
root.geometry("300x300")

app = GUI(root)
root.mainloop()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241