What i expect to happen:
- Window is opened with a button in it
- When pressing the button the button is being deleted
- After the button was deleted a countdown starts
- After the countdown finished the program closes itself
What actually happens:
- Window is opened with a button in it
- When you press the button a countdown starts
- After the countdown finished the button is deleted
- Program closes itself
Also the button is not only not being destroyed but it seems like the entire window is freezing.
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()
CodePudding user response:
You can call the update()
method and then the button will be deleted first.
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()