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

强颜欢笑 提交于 2019-12-07 01:48:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!