How to change the menu background color of Tkinter's OptionMenu widget?

TkinterPythonGUI-Programming

Consider a scenario where we need something to display a menu with some choices in the form of a dropdown list. To achieve this particular feature, Tkinter provides an OptionMenu widget which consists of features to add choices and a list of items in it. We can set up the default behavior of the OptionMenu widget by configuring its property such as background color, width, height, foreground color, etc.

Example

# Import the required libraries
from tkinter import *
from PIL import Image, ImageTk

# Create an instance of tkinter frame
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Add a Label
Label(win, text="Select a Day from the Menu", font=('Aerial 13')).pack(pady=10)

# Create a Variable to store the selection
var = StringVar()

# Create an OptionMenu Widget and add choices to it
option = OptionMenu(win, var, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
option.config(bg="gray81", fg="white")
option['menu'].config(bg="green3")
option.pack(padx=20, pady=30)

win.mainloop()

Output

Running the above code will display an OptionMenu having Days as the choices. The Menu has some background color and foreground color which can be changed in the configuration method.

raja
Published on 08-Jun-2021 09:23:36
Advertisements