简体   繁体   中英

tkinter: change foreground color of specific menu items

With TKinter, is it possible to change the text color of only certain items in a menu? I want to make less popular items have less contrast so users can quickly find the most frequently used items. So far, I've only found Menu 's foreground option, which changes the color of everything (not particular items), or making the specific items' state option disabled to change the color, but they should still be able to be clicked to do something. Any tips? I'm hoping there's a way I could do this using the Menu or Menubutton classes without having to re-implement a menu from scratch for this one feature.

I think you're looking for the entryconfig method, which works by passing the index of the menu command, then an option from the list of add method options . Here's an example:

root = Tk()

menubar = Menu(root)

filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open")
filemenu.add_command(label="Save")
menubar.add_cascade(label="File", menu=filemenu)

#entryconfig method to change the item with index '0' to white
filemenu.entryconfig(0, foreground='white')

root.config(menu=menubar)
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM