我尝试在用 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()
当您在输入框中按 Enter 时,计时器应该启动,但它却显示此错误
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'
我尝试过将计时器转换为字符串和浮动
str(timer).after(1000, countdown()) in line11
float(timer).after(1000, countdown()) in line11
如果这 3 个属性(int、str 和 float)不起作用,则与“entry”一起使用。
或者如何为我的游戏添加计时器?
素胚勾勒不出你
相关分类