我期望發生的事情:
- 視窗打開,里面有一個按鈕
- 按下按鈕時,按鈕被洗掉
- 洗掉按鈕后開始倒計時
- 倒計時結束后程式自行關閉
實際發生的情況:
- 視窗打開,里面有一個按鈕
- 當您按下按鈕時,倒計時開始
- 倒計時完成后按鈕被洗掉
- 程式自行關閉
此外,按鈕不僅沒有被破壞,而且似乎整個視窗都在凍結。
from tkinter import *
import time
count = 5
window = Tk()
def func():
global count
button.destroy() #This should destroy the button but it stays there until the while loop is finished
while count > 0:
print(count)
count = count - 1
time.sleep(1)
quit()
button = Button(text="text", command=func)
button.pack()
window.mainloop()
uj5u.com熱心網友回復:
您可以呼叫該update()方法,然后將首先洗掉該按鈕。
from tkinter import *
import time
count = 5
window = Tk()
def func():
global count
button.destroy() #This should destroy the button but it stays there until the while loop is finished
window.update()
while count > 0:
print(count)
count = count - 1
time.sleep(1)
quit()
button = Button(text="text", command=func)
button.pack()
window.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497215.html
