2 回答

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是因?yàn)楫?dāng)按鈕被禁用時(shí) tkinter 沒有控制,所以它沒有更新。例如,您需要button.update()在禁用后調(diào)用以強(qiáng)制更新:
def button_click():
button["state"] = DISABLED
button.update() # force the update
print("button clicked! Please wait 1 second...")
time.sleep(1)
button["state"] = NORMAL
但是,最好使用after()而不是time.sleep():
def button_click():
button["state"] = DISABLED
print("button clicked! Please wait 1 second...")
# enable the button after one second
button.after(1000, lambda: button.config(state='normal'))

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
也許您忘記了代碼末尾的“root.mainloop”。
import time
from tkinter import *
def button_click():
button["state"] = DISABLED
print("button clicked! Please wait 1 second...")
time.sleep(1)
button["state"] = NORMAL
root = Tk()
button = Button(root, text="Click Me!", command=button_click)
button.pack()
root.mainloop()
這對(duì)我有用。您只能每 1 秒按下一次按鈕。
添加回答
舉報(bào)