如何更改 Tkinter 的 OptionMenu 小部件的菜单背景颜色? [英] How to change menu background color of Tkinter's OptionMenu widget?

查看:38
本文介绍了如何更改 Tkinter 的 OptionMenu 小部件的菜单背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我以 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 小部件的菜单背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆