Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

tkinter destroy method not working the right way

What i expect to happen:

  1. Window is opened with a button in it
  2. When pressing the button the button is being deleted
  3. After the button was deleted a countdown starts
  4. After the countdown finished the program closes itself

What actually happens:

  1. Window is opened with a button in it
  2. When you press the button a countdown starts
  3. After the countdown finished the button is deleted
  4. Program closes itself

Also the button is not only not being destroyed but it seems like the entire window is freezing.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading