Exploring Color Customization in Tkinter
Last Updated : 24 Apr, 2024
One of the key elements in designing attractive and user-friendly interfaces is the use of Tkinter colors. In this article, we'll discuss the various aspects of using colors in Tkinter applications, covering syntax, code examples, and practical implementations.
How to Change Color Tkinter?
Tkinter colors refer to the various color options available in the Tkinter library, which is a standard GUI (Graphical User Interface) toolkit in Python. Tkinter provides developers with the ability to customize the appearance of GUI elements such as buttons, labels, and text fields by specifying colors for backgrounds, foregrounds (text), highlights, and selection states.
Colors are essential for enhancing the visual appeal and user experience of graphical user interfaces (GUIs). In Tkinter, the popular Python GUI toolkit, you have access to a range of color options to tailor the appearance of widgets. Let's delve into the common color options and how they are utilized in Tkinter.
- Active Background and Foreground Colors :
activebackground
: Sets the background color when a widget is active, like during user interaction. activeforeground
: Determines the foreground color when the widget is active, providing visual feedback.
- Background and Foreground Colors :
background (or bg)
: Sets the background color of the widget's surface. foreground (or fg)
: Defines the foreground color for text or other elements within the widget.
- Disabled State Colors :
disabledforeground
: Determines the foreground color when the widget is disabled, aiding in distinguishing disabled widgets. highlightbackground
: Sets the background color of the highlight region when the widget has focus. highlightcolor
: Defines the foreground color of the highlight region when the widget has focus.
- Selection Colors :
selectbackground
: Sets the background color for selected items within the widget, like selected text in an Entry widget or selected options in a Listbox. selectforeground
: Defines the foreground color for selected items within the widget.
Python Tkinter Colors Examples
RGB Color Representation in Python Tkinter
RGB (Red, Green, Blue) values are a common way to represent colors in digital systems. In Tkinter, you can specify colors using RGB values ranging from 0 to 255 for each component.
Syntax
# Replace RR, GG, BB with hexadecimal values
widget.configure(bg='#RRGGBB')
Example: In this example, below code creates a Tkinter window and sets its title. Then, it configures the window's background color to light green using RGB values. Finally, it starts the Tkinter event loop to display the window.
Python3 from tkinter import * root = Tk() root.title("Tkinter Colors RGB Example") # Set window background color using RGB values root.configure(bg='#90ee90') # lightgreen color root.mainloop()
Output
Tkinter Colors RGB
Customizing Entry Widgets with Color Options in Tkinter
A Tkinter Entry widget is a single-line text entry field that allows users to input text, and it can be customized with various color options to control its appearance.
Syntax
# Replace parent and options according to preferences
entry_widget = Entry(parent, options)
Example: In this example, below code creates a Tkinter window with a titled header and an Entry widget for text input. The Entry widget has customized selection colors: light blue for the background and black for the text.
Python3 from tkinter import * root = Tk() root.title("Tkinter Color Text Example") # Create an Entry widget with selection colors entry = Entry(root, selectbackground="lightblue", selectforeground="black") entry.pack() root.mainloop()
Output

Creating Colorful Text in Python Tkinter
Changing the color of text in Tkinter is very easy. You can set the foreground (text) color using the fg
parameter.
Syntax
# Change text color to blue
Label(fg='blue')
Example: In this example, below code creates a Tkinter window and sets its title. Then, it creates a label widget with the text "Hello, World!" displayed in blue color, and packs it into the window. Finally, it starts the Tkinter event loop to show the window.
Python3 from tkinter import * root = Tk() root.title("Tkinter Color Text Example") # Create a label with colored text label = Label(root, text="Hello, World!", fg='blue') label.pack() root.mainloop()
Output

Buttons are essential GUI elements, and you can customize their colors to match your application's theme.
Syntax
# Change button background color to red
Button.config(bg='red')
Example: In this example, below code creates a Tkinter window with a specified title. It then generates a button labeled "Click Me" with a red background color and places it within the window. Finally, it enters the Tkinter event loop to display the window and handle user interactions.
Python3 from tkinter import * root = Tk() root.title("Tkinter Color Button Example") # Create a button with active background and foreground colors button = Button(root, text="Click Me", activebackground="blue", activeforeground="white") button.pack() root.mainloop()
Output

Python Tkinter Color Chooser
Tkinter provides a color chooser dialog, allowing users to pick colors interactively.
Syntax
from tkinter import colorchooser
# Returns a tuple of (color, hex)
color = colorchooser.askcolor(title="Choose color")
Example: In this example, below code creates a Tkinter window titled "Tkinter Color Chooser Example" and a button labeled "Choose Color". When the button is clicked, it opens a color chooser dialog. The hexadecimal value of the selected color is printed to the console
Python3 from tkinter import * from tkinter import colorchooser root = Tk() root.title("Tkinter Color Chooser Example") def choose_color(): color = colorchooser.askcolor(title="Choose color") print("Selected color:", color[1]) # Print the hexadecimal color value button = Button(root, text="Choose Color", command=choose_color) button.pack() root.mainloop()
Output
Tkinter Color Chooser Example Adding Color to Labels in Python Tkinter
Labels are often used to display text or images, and you can style them with various colors.
Syntax
# Change label background color to yellow
Label(bg='yellow')
Example: In this example, below code creates a Tkinter window titled "Tkinter Color Label Example". It creates a label widget displaying the text "Colorful Label" with a yellow background color, and packs it into the window.
Python3 from tkinter import * root = Tk() root.title("Tkinter Color Label Example") # Create a label with background and foreground colors label = Label(root, text="Hello, Tkinter!", bg="lightgray", fg="black") label.pack() root.mainloop()
Output
Tkinter Color label ExamplePython Tkinter Color Window
You can also set the background color of the entire Tkinter window.
Syntax
# Change window background color to light gray
root.configure(bg='lightgray')
Example: In this example, below code creates a Tkinter window titled "Tkinter Color Window Example" and sets its background color to light gray. Then, it enters the Tkinter event loop to display the window
Python3 from tkinter import * root = Tk() root.title("Tkinter Color Window Example") # Set window background color root.configure(bg='lightgray') root.mainloop()
Output
Tkinter Color Window Example
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read