- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to dynamically add/remove/update labels in a Tkinter window?
We can use the Tkinter Label widget to display text and images. By configuring the label widget, we can dynamically change the text, images, and other properties of the widget.
To dynamically update the Label widget, we can use either config(**options) or an inline configuration method such as for updating the text, we can use Label["text"]=text; for removing the label widget, we can use pack_forget() method.
Example
# Import the required libraries from tkinter import * from tkinter import ttk from PIL import ImageTk, Image # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") def add_label(): global label label=Label(win, text="1. A Newly created Label", font=('Aerial 18')) label.pack() def remove_label(): global label label.pack_forget() def update_label(): global label label["text"]="2. Yay!! I am updated" # Create buttons for add/remove/update the label widget add=ttk.Button(win, text="Add a new Label", command=add_label) add.pack(anchor=W, pady=10) remove=ttk.Button(win, text="Remove the Label", command=remove_label) remove.pack(anchor=W, pady=10) update=ttk.Button(win, text="Update the Label", command=update_label) update.pack(anchor=W, pady=10) win.mainloop()
Running the above code will display a window with some buttons in it. Each button can be used to update/remove or add a label in the application.
Output
Upon clicking the "Update the Label" button, the label will get updated as follows −
- Related Articles
- How to add a margin to a tkinter window?
- Dynamically Resize Buttons When Resizing a Window using Tkinter
- Add and Remove Views in Android Dynamically?
- How to dynamically update a listView in Android?
- How to remove the title bar in a Tkinter window without using overrideredirect() method?
- How to dynamically update a ListView on Android
- Add and Remove Views in Android Dynamically in Kotlin?
- How to generate Tkinter Buttons dynamically?
- How to add a button dynamically in android?
- How to dynamically update a ListView on Android Kotlin?
- How To Dynamically Resize Button Text in Tkinter?
- How to dynamically update a plot in a loop in Ipython notebook?
- Tkinter-How to get the current date to display in a tkinter window?
- How to update a Button widget in Tkinter?
- How to show webcam in TkInter Window?

Advertisements