Tkinter

How To Show/Hide a Label in Tkinter After Pressing a Button

In this tutorial, we are going to see how to show/hide a label in Tkinter after pressing a button in Python. For this we will use the pack_forget() method.

If we want to hide a widget from the screen or top level, the forget() method is used. There are two types of methods forget_pack() (similar to forget()) and forget_grid() which are used with pack() and grid() methods respectively.
 

 

How To Show/Hide a Label in Tkinter After Pressing a Button
import tkinter as tk

root = tk.Tk()
root.geometry('200x150')  

btn1 = tk.Button(root, text='Show', command=lambda: label.pack())       
btn1.pack(pady=20)

btn2 = tk.Button(root, text='Hide', command=lambda: label.pack_forget())
btn2.pack()

label = tk.Label(root, text = "Welcome to StackHowTo!")
label.pack()

root.mainloop()

Output:


 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *