Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to fully change the color of a Tkinter Listbox?
Tkinter Listbox widgets are very useful for displaying a large set of data items in list format. To change the appearance of the entire Listbox, including background and text colors, we can use the configure(**options) method to modify the widget's properties.
Basic Color Configuration
The most common approach is using the configure() method to set background, foreground, and other visual properties −
# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Add a Listbox widget with programming languages as list items
listbox = Listbox(win)
listbox.insert(END, "C++", "Java", "Python", "Rust", "GoLang", "Ruby", "JavaScript", "C#", "SQL", "Dart")
listbox.pack(side=LEFT, fill=BOTH)
# Configure colors and font
listbox.configure(background="skyblue4", foreground="white", font=('Arial', 13))
win.mainloop()
Advanced Color Customization
You can also customize selection colors and other visual elements −
from tkinter import *
win = Tk()
win.geometry("700x350")
win.title("Customized Listbox Colors")
# Create a Listbox with advanced color configuration
languages = ["Python", "Java", "C++", "JavaScript", "Ruby", "Go", "Rust", "Swift"]
listbox = Listbox(win,
background="darkblue", # Background color
foreground="yellow", # Text color
selectbackground="orange", # Selection background
selectforeground="black", # Selection text color
font=('Courier', 12), # Font style
height=8, # Number of visible items
activestyle='dotbox') # Active item style
# Insert items
for lang in languages:
listbox.insert(END, lang)
listbox.pack(pady=20, padx=20, fill=BOTH, expand=True)
win.mainloop()
Available Color Options
| Option | Description | Example Values |
|---|---|---|
background / bg
|
Background color of the listbox | "white", "#FF5733", "lightgray" |
foreground / fg
|
Text color of items | "black", "#000000", "darkred" |
selectbackground |
Background color when item is selected | "blue", "#4CAF50", "yellow" |
selectforeground |
Text color when item is selected | "white", "black", "#FFFFFF" |
Using Hex Colors
For precise color control, you can use hexadecimal color codes −
from tkinter import *
win = Tk()
win.geometry("500x400")
# Create listbox with hex color codes
colors_listbox = Listbox(win)
# Sample color names
color_names = ["Red", "Green", "Blue", "Purple", "Orange", "Pink"]
for color in color_names:
colors_listbox.insert(END, color)
# Configure with hex colors
colors_listbox.configure(
bg="#2C3E50", # Dark blue-gray background
fg="#ECF0F1", # Light gray text
selectbackground="#E74C3C", # Red selection background
selectforeground="#FFFFFF", # White selection text
font=('Verdana', 11)
)
colors_listbox.pack(pady=30, padx=30, fill=BOTH, expand=True)
win.mainloop()
Conclusion
Use the configure() method to fully customize Listbox colors including background, foreground, and selection colors. Combine different color options with fonts to create visually appealing interfaces that match your application's theme.
