Ahmed4end
Ahmed4end

Reputation: 324

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 when method label.after(1000 , label.destroy) doesn't work?

import tkinter, win32api, win32con, pywintypes

label = tkinter.Label(text='Text on the screen', font=('Times New Roman','80'), fg='black', bg='white')
label.master.overrideredirect(True)
label.master.geometry("+250+250")
label.master.lift()
label.master.wm_attributes("-topmost", True)
label.master.wm_attributes("-disabled", True)
label.master.wm_attributes("-transparentcolor", "white")

hWindow = pywintypes.HANDLE(int(label.master.frame(), 16))
exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT
win32api.SetWindowLong(hWindow, win32con.GWL_EXSTYLE, exStyle)

label.pack()

label.after(1000 , lambda: label.destroy())   #doesn't work anyway..

label.mainloop()

Upvotes: 3

Views: 72819

Answers (2)

Mike - SMT
Mike - SMT

Reputation: 15226

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

Upvotes: 5

TorrentialTerminal
TorrentialTerminal

Reputation: 49

import tkinter
import time

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

Upvotes: 4

Related Questions