2 回答

TA貢獻1993條經驗 獲得超6個贊
這是因為當按鈕被禁用時 tkinter 沒有控制,所以它沒有更新。例如,您需要button.update()在禁用后調用以強制更新:
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貢獻1827條經驗 獲得超4個贊
也許您忘記了代碼末尾的“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()
這對我有用。您只能每 1 秒按下一次按鈕。
添加回答
舉報