问题
If I take a simple example of OptionMenu from http://effbot.org/tkinterbook/optionmenu.htm, and add a line that sets background color (see below), only the button background changes color, not the drop-down menu which remains gray. Can I set color for both the button and the menu of OptionMenu?
I am using Windows 7, Python 2.6.6, Tkinter Rev 73770
from Tkinter import *
master = Tk()
variable = StringVar(master)
variable.set("one") # default value
w = OptionMenu(master, variable, "one", "two", "three")
w.config(bg = "GREEN") # Set background color to green
w.pack()
mainloop()
Thank you
回答1:
You need to grab the menu
object from the OptionMenu
and set its background color. This should accomplish what you want...
w = OptionMenu(master, variable, "one", "two", "three")
w.config(bg = "GREEN") # Set background color to green
# Set this to what you want, I'm assuming "green"...
w["menu"].config(bg="GREEN")
w.pack()
来源:https://stackoverflow.com/questions/6178153/how-to-change-menu-background-color-of-tkinters-optionmenu-widget