How to hide a widget after some time in Python Tkinter?

TkinterPythonGUI-Programming

Tkinter is a standard Python library for developing GUI-based applications. We can create games, tools, and other applications using the Tkinter library. To develop GUI-based applications, Tkinter provides widgets.

Sometimes, there might be a requirement to hide a widget for some time. This can be achieved by using the pack_forget() method. When we pack the widget in the window using the various methods, we have to use the same method for hiding the widget.

Example

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

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

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

# Create a canvas widget
canvas=Canvas(win, width=400, height=300)
canvas.pack()

# Add an image in the canvas widget
img=ImageTk.PhotoImage(file="baseball.png")
canvas.create_image(100, 150,image=img)

# Hide the image from the canvas after sometime
canvas.after(3000, canvas.pack_forget)

win.mainloop()

Output

Running the given code will display an image in the Canvas widget that will disappear after sometime.

raja
Published on 05-Aug-2021 14:48:24
Advertisements