Tkinter中的多彩下拉菜单 [英] Colorful dropdown menu in tkinter

查看:64
本文介绍了Tkinter中的多彩下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Tkinter的下拉菜单中添加彩色项目?下拉列表将从枚举类中获取其值.例如,枚举类具有红色,黄色,绿色"元素.根据元素,下拉部分将为绿色,红色,黄色.我该怎么做?

Is it possible to add colorful items in dropdown in Tkinter? Dropdown will take its values from enum class. For example, enum class has 'red,yellow,green' elements. Sections of dropdown will be green, red, yellow colors according to element. How can i make that?

推荐答案

使用前景和背景关键字可以在菜单中添加颜色.看看.

Adding colors to the menu is possible with the keywords foreground and background; take a look.

try:
    import tkinter as tk
except:
    import Tkinter as tk

root = tk.Tk()

menubar = tk.Menu(root)

filemenu = tk.Menu(menubar,tearoff=0)

filemenu.add_command(label="text 1")
filemenu.add_command(label='text 2', foreground = 'blue')
filemenu.add_command(label='text 3',background='green')

# entire dropdown menu has been given this color
editmenu = tk.Menu(menubar,tearoff=0,foreground = 'green')
editmenu.add_command(label='copy')
editmenu.add_command(label='paste')

menubar.add_cascade(menu=filemenu, label="File")
menubar.add_cascade(menu=editmenu, label="edit")

root.config(menu=menubar)

root.mainloop()

从预定义的列表中选取颜色,涉及一个for循环.

Taking colors from a predefined list, involves a for-loop.

try:
    import tkinter as tk
except:
    import Tkinter as tk

root = tk.Tk()

menubar = tk.Menu(root,foreground='red')
filemenu = tk.Menu(menubar,tearoff=0)

text = ['text 1', 'text 2', 'text 3']
color = ['green','red','yellow']

for a,b in zip(text,color):
    filemenu.add_command(label=a,foreground=b)

menubar.add_cascade(menu=filemenu, label="File")
root.config(menu=menubar)
root.mainloop()

希望将代码转换为2.7.

hopefully converted code to 2.7.

这篇关于Tkinter中的多彩下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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