Showing and Hiding widgets in Tkinter?

Let us suppose that we have to create an application such that we can show as well as hide the widgets whenever we need.

  • The widgets can be hidden through pack_forget() method.

  • To show the hidden widgets, we can use the pack() method.

Both methods can be invoked using the lambda or anonymous function.


#Import the required library
from tkinter import *

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

#Define the geometry of the window

#Define function to hide the widget
def hide_widget(widget):

#Define a function to show the widget
def show_widget(widget):

#Create an Label Widget
label= Label(win, text= "Showing the Message", font= ('Helvetica bold', 14))

#Create a button Widget
button_hide= Button(win, text= "Hide", command= lambda:hide_widget(label))

button_show= Button(win, text= "Show", command= lambda:show_widget(label))



Running the above code will display a window with two buttons “Show” and “Hide” which can be used to show and hide the widgets.

Now click on “Hide” button to hide the Label Text and “Show” to show the Label Text.