Skip to content Skip to sidebar Skip to footer

How To Delete Or Destroy Label In Tkinter?

This Tkinter code doesn't have a widget, just a label so it displays just a text on the screen so I want to destroy or delete the label after a certain time !. How can I do this wh

Solution 1:

In the code you have provided I believe the fix you are looking for is to change this:

label.after(1000 , lambda: label.destroy())

To this:

label.after(1000, label.master.destroy)

You need to destroy label.master (I am guessing this is actually a root window) because if you do not then you end up with a big box on the screen that is not transparent.

That said I am not sure why you are writing your app in this way. I guess it works and I was not actually aware you could do this but still I personally would write it using a root window to work with.

import tkinter as tk

root = tk.Tk()


label = tk.Label(root, text='Text on the screen',
                 font=('Times New Roman','80'), fg='black', bg='white')
label.pack()

root.overrideredirect(True)
root.geometry("+250+250")
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")

root.after(1000, root.destroy)

root.mainloop()

Solution 2:

import tkinter
importtimeroot=Tk()

label = Label(root, text="Text on the screen", font=('Times New Roman',  '80'), fg="black", bg="white")

time.sleep(1000)

label.destroy()

root.mainloop()

Post a Comment for "How To Delete Or Destroy Label In Tkinter?"