亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

AttributeError:“int”對象沒有“after”屬性

AttributeError:“int”對象沒有“after”屬性

慕無忌1623718 2023-08-22 17:53:57
我嘗試在用 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器?
查看完整描述

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 毫秒之前不會調用該命令。


查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號