Home > Back-end >  tkinter destroy method not working the right way
tkinter destroy method not working the right way

Time:06-29

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.

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