问题描述
如果我以 http://effbot.org/tkinterbook/optionmenu.htm 中的 OptionMenu 为例,并添加一行设置背景颜色(见下文),只有按钮背景改变颜色,而不是保持灰色的下拉菜单.OptionMenu 的按钮和菜单可以同时设置颜色吗?
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?
我使用的是 Windows 7、Python 2.6.6、Tkinter Rev 73770
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()
谢谢
推荐答案
您需要从 OptionMenu
中获取 menu
对象并设置其背景颜色.这应该完成你想要的......
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()
这篇关于如何更改 Tkinter 的 OptionMenu 小部件的菜单背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!