简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM