Deleting a Label in Python Tkinter



Tkinter label widgets are used to display text and images in the application. We can also configure the properties of Label widget that are created by default in a tkinter application.

If we want to delete a label that is defined in a tkinter application, then we have to use the destroy() method.

Example

In this example, we will create a Button that will allow the user to delete the label from the widget.

# Import the required libraries
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.geometry("700x350")

def on_click():
   label.after(1000, label.destroy())

# Create a Label widget
label = Label(win, text=" Deleting a Label in Python Tkinter", font=('Helvetica 15'))
label.pack(pady=20)

# Add a Button to Show/Hide Canvas Items
ttk.Button(win, text="Delete", command=on_click).pack()

win.mainloop()

Output

If we run the above code, it will display a window with a label widget and a Button.

Now, click the button to delete the Label from the window.


Advertisements