I tried adding a timer to a game I made with Tkinter
from tkinter import *
timer = 60
def startGame(event):
if timer == 60:
countdown()
def countdown():
global timer
if timer > 0:
timer -= 1
timeLabel.config(text='Time left:' + str(timer))
timer.after(1000, countdown())
window = Tk()
timeLabel = Label(window, text='time = 60 sec')
entryBox = Entry(window)
timeLabel.pack()
entryBox.pack()
window.bind('<Return>', startGame)
entryBox.pack
entryBox.focus_set()
window.mainloop()
the timer is supposed to start when you press enter in the entryBox but instead, it shows this error
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/user/PycharmProjects/Game/example.py", line 5, in startGame
countdown()
File "/home/user/PycharmProjects/Game/example.py", line 11, in countdown
timer.after(1000, countdown())
AttributeError: 'int' object has no attribute 'after'
I have tried converting timer to string and float by
str(timer).after(1000, countdown()) in line11
float(timer).after(1000, countdown()) in line11
if these 3 attributes(int, str, and float) don't work which will work with 'entry'.
Or how can I add a timer to my game?