- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 change the color of a Tkinter label programmatically?
Tkinter Label widgets are used to add text or images to the application. We can even configure the basic properties of labels using the config(options) method. Generally, in order to configure the widgets property dynamically, we use callback functions where we modify the value of attributes.
Example
In this example, we will modify the color Tkinter Labels by defining the callback function. The function can be activated by a button that forces the labels to change the color.
#Import required libraries from tkinter import * from tkinter import ttk #Create an instance of tkinter frame win= Tk() #Define the geometry of the window win.geometry("750x250") #Define a function to Change the color of the label widget def change_color(): label.config(bg= "gray51", fg= "white") #Create a label label= Label(win, text= "Hey There! How are you?", font= ('Helvetica20 italic')) label.pack(pady=30) #Create a Button ttk.Button(win, text="Change Color", command=change_color).pack(pady=20) win.mainloop()
Output
Running the above code will display a window that contains a label and a button.
Now, click "Change Color" button to change the color of the Label widget.
- Related Articles
- How to fully change the color of a Tkinter Listbox?
- How to change the size of text on a label in Tkinter?
- How to change the background color of a Treeview in Tkinter?
- How to change the color of a Tkinter rectangle on clicking?
- How to change the background color of a tkinter Canvas dynamically?
- How to change the color of ttk button in Tkinter?
- How to change Tkinter label text on button press?
- How to change the mouse pointer color in Tkinter?
- How to change text cursor color in Tkinter?
- How to change the color of certain words in a Tkinter text widget?
- How to change the color of X-axis label using ggplot2 in R?
- How to make a Button Hover to change the Background Color in Tkinter?
- How to change the menu background color of Tkinter's OptionMenu widget?
- Python Tkinter – How do I change the text size in a label widget?
- Change the color of "tab header" in ttk.Notebook (tkinter)

Advertisements