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
Selected Reading
How to change the menu background color of Tkinter's OptionMenu widget?
The OptionMenu widget in Tkinter creates dropdown menus with selectable choices. You can customize both the button appearance and the dropdown menu's background color using configuration methods.
Basic OptionMenu with Custom Colors
Here's how to change both the button and menu background colors −
import tkinter as tk
# Create main window
root = tk.Tk()
root.geometry("400x300")
root.title("OptionMenu Color Example")
# Create StringVar to store selection
selected_day = tk.StringVar()
selected_day.set("Select a day")
# Create OptionMenu with days
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
option_menu = tk.OptionMenu(root, selected_day, *days)
# Configure button background and text color
option_menu.config(bg="lightblue", fg="darkblue", font=("Arial", 12))
# Configure dropdown menu background
option_menu['menu'].config(bg="lightgreen", fg="black")
option_menu.pack(pady=50)
root.mainloop()
Advanced Styling Options
You can customize various aspects of the OptionMenu appearance −
import tkinter as tk
root = tk.Tk()
root.geometry("500x350")
root.title("Styled OptionMenu")
# Add a label
label = tk.Label(root, text="Choose your favorite color:", font=("Arial", 14))
label.pack(pady=20)
# Create variable
color_var = tk.StringVar()
color_var.set("Colors")
# Color options
colors = ["Red", "Blue", "Green", "Yellow", "Purple", "Orange"]
# Create OptionMenu
color_menu = tk.OptionMenu(root, color_var, *colors)
# Style the button
color_menu.config(
bg="navy", # Button background
fg="white", # Button text color
font=("Arial", 12, "bold"),
activebackground="darkblue", # Color when hovered
activeforeground="yellow", # Text color when hovered
width=15, # Button width
relief="raised", # Border style
bd=3 # Border width
)
# Style the dropdown menu
color_menu['menu'].config(
bg="lightcyan", # Menu background
fg="darkgreen", # Menu text color
font=("Arial", 11),
activebackground="yellow", # Highlighted option background
activeforeground="red" # Highlighted option text
)
color_menu.pack(pady=30)
# Display selection
def show_selection():
selection_label.config(text=f"Selected: {color_var.get()}")
selection_label = tk.Label(root, text="Selected: None", font=("Arial", 12))
selection_label.pack(pady=10)
# Button to show selection
show_btn = tk.Button(root, text="Show Selection", command=show_selection)
show_btn.pack(pady=10)
root.mainloop()
Configuration Options
| Property | Description | Example Values |
|---|---|---|
bg |
Button background color | "red", "#FF0000", "lightblue" |
fg |
Button text color | "white", "black", "#00FF00" |
activebackground |
Color when button is pressed | "darkblue", "gray" |
font |
Text font and size | ("Arial", 12), ("Times", 14, "bold") |
Key Points
- Use
option.config(bg="color")to change the button background - Use
option['menu'].config(bg="color")to change the dropdown menu background - The
activebackgroundproperty controls hover effects - You can use color names, hex codes, or RGB values for colors
Conclusion
Customizing OptionMenu colors involves configuring both the button and menu components separately. Use config() for the button styling and ['menu'].config() for the dropdown menu appearance to create visually appealing interfaces.
Advertisements
