Changing the colour of Tkinter Menu Bar

Python's Tkinter library provides a simple way to create graphical user interfaces (GUIs). One common customization requirement is changing the color of the menu bar to match your application's theme. By default, Tkinter menu bars have a standard appearance, but you can modify their colors using specific configuration options.

In this tutorial, we will explore different methods to customize the color of the Tkinter menu bar using widget?specific options and theme configurations.

Method 1: Using Widget?Specific Options

The most straightforward approach is to use the bg (background) option directly on the menu widget. This allows you to set a custom background color for the menu bar ?

import tkinter as tk

# Create a new Tkinter window
window = tk.Tk()
window.title("Menu Bar Color Example")
window.geometry("400x300")

# Create a menu bar
menu_bar = tk.Menu(window)

# Create File menu
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New")
file_menu.add_command(label="Open")
file_menu.add_command(label="Save")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=window.quit)

# Create Edit menu
edit_menu = tk.Menu(menu_bar, tearoff=0)
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Paste")

# Add menus to menu bar
menu_bar.add_cascade(label="File", menu=file_menu)
menu_bar.add_cascade(label="Edit", menu=edit_menu)

# Configure the background color of the menu bar
menu_bar.configure(bg="lightblue", fg="darkblue")

# Attach the menu bar to the window
window.config(menu=menu_bar)

# Run the Tkinter event loop
window.mainloop()

In this example, we create a complete menu bar with File and Edit menus, then configure both background (bg) and foreground (fg) colors. The bg option sets the background color, while fg sets the text color.

Method 2: Using TTK Themes and Style Configurations

For more extensive customization, you can use the ttk module with themes and style configurations. This approach provides better integration with the operating system's native look and feel ?

import tkinter as tk
from tkinter import ttk

# Create a new Tkinter window
window = tk.Tk()
window.title("TTK Styled Menu Bar")
window.geometry("400x300")

# Configure TTK style
style = ttk.Style()
style.theme_use("clam")

# Configure menu bar style
style.configure("TMenubar", background="darkgreen", foreground="white")

# Create a menu bar using ttk
menu_bar = tk.Menu(window)

# Create menus with custom colors
file_menu = tk.Menu(menu_bar, tearoff=0, bg="lightgreen", fg="black")
file_menu.add_command(label="New")
file_menu.add_command(label="Open")
file_menu.add_command(label="Save")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=window.quit)

view_menu = tk.Menu(menu_bar, tearoff=0, bg="lightgreen", fg="black")
view_menu.add_command(label="Zoom In")
view_menu.add_command(label="Zoom Out")
view_menu.add_command(label="Full Screen")

# Add menus to menu bar
menu_bar.add_cascade(label="File", menu=file_menu)
menu_bar.add_cascade(label="View", menu=view_menu)

# Configure the menu bar colors
menu_bar.configure(bg="darkgreen", fg="white")

# Attach the menu bar to the window
window.config(menu=menu_bar)

# Run the Tkinter event loop
window.mainloop()

This method uses TTK themes for a more polished appearance. The style.configure() method allows you to customize the overall theme, while individual menu colors can still be set using the bg and fg options.

Comparison

Method Complexity Customization Level Best For
Widget Options Simple Basic colors Quick color changes
TTK Themes Moderate Full styling Professional applications

Conclusion

You can customize Tkinter menu bar colors using either widget?specific options for simple changes or TTK themes for comprehensive styling. Use the bg and fg options for basic customization, or combine TTK themes with individual menu styling for more professional?looking applications.

Updated on: 2026-03-27T08:56:56+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements