我嘗試在用 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”一起使用?;蛘呷绾螢槲业挠螒蛱砑佑嫊r器?
AttributeError:“int”對象沒有“after”屬性
慕無忌1623718
2023-08-22 17:53:57