我尝试在用 Tkinter 制作的游戏中添加计时器from tkinter import *timer = 60def 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.packentryBox.focus_set()window.mainloop()当您在输入框中按 Enter 时,计时器应该启动,但它却显示此错误Exception in Tkinter callbackTraceback (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 line11float(timer).after(1000, countdown()) in line11如果这 3 个属性(int、str 和 float)不起作用,则与“entry”一起使用。或者如何为我的游戏添加计时器?
1 回答
素胚勾勒不出你
TA贡献1827条经验 获得超9个赞
摆脱所有混乱after()
是一种方法root
,而不是timer
您的情况中的方法。只需替换timer.after(...)
为root.after(...)
:
if timer > 0: timer -= 1 timeLabel.config(text='Time left:' + str(timer)) timer.after(1000, countdown)
确保()
从 中删除 , countdown()
,以便在 1000 毫秒之前不会调用该命令。
添加回答
举报
0/150
提交
取消