Open In App

How to change background color of Tkinter OptionMenu widget?

Last Updated : 07 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Tkinter

While creating GUI applications, there occur various instances in which you need to make selections among various options available. For making such choices, the Option Menu widget was introduced. In this article, we will be discussing the procedure of changing menu background color of Tkinter’s option Menu widget. 

To achieve our required functionality a regular OptionMenu is first set up and then color is added and changed using config() method.

Syntax:

w = OptionMenu(app, #Options Menu widget name, “#Opton1”, “#Option2”, “#Option3”)

w.config(bg = “#Background Color of Options Menu”, fg=”#Text Color”)

Functions Used

  • OptionMenu() is used to create a dropdown menu

Syntax:

OptionMenu(master,options)

Parameters:

  • master: This parameter is used to represents the parent window.
  • options: Contain the Menu values
  • config() is used to set up the color change

Approach

  • Import module
  • Now, create a GUI app using tkinter
  • Next, give a title to the app(optional).
  • Then, create an Options Menu widget.
  • Moreover, create the Displayed Options for Options Menu widget.
  • Further, set the menu background color.
  • Assign the text you want to appear when Options Menu is not open
  • Set the background color of Displayed Options.
  • Display the Options Menu widget in GUI
  • Finally, make the loop for displaying the GUI app on the screen

Program:

Python




# Python program to change menu background
# color of Tkinter's Option Menu
 
# Import the library tkinter
from tkinter import *
 
# Create a GUI app
app = Tk()
 
# Give title to your GUI app
app.title("Vinayak App")
 
# Construct the label in your app
l1 = Label(app, text="Choose the week day here")
 
# Display the label l1
l1.grid()
 
# Construct the Options Menu widget in your app
text1 = StringVar()
 
# Set the value you wish to see by default
text1.set("Choose here")
 
# Create options from the Option Menu
w = OptionMenu(app, text1, "Sunday", "Monday", "Tuesday",
               "Wednesday", "Thursday", "Friday", "Saturday")
 
# Se the background color of Options Menu to green
w.config(bg="GREEN", fg="WHITE")
 
# Set the background color of Displayed Options to Red
w["menu"].config(bg="RED")
 
# Display the Options Menu
w.grid(pady=20)
 
# Make the loop for displaying app
app.mainloop()


Output:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads