Tkinter add_cascade option_add

Eric Brunel eric_brunel at despammed.com
Thu Sep 15 03:09:06 EDT 2005


On Wed, 14 Sep 2005 09:58:25 -0600, Bob Greschke <bob at passcal.nmt.edu> wrote:
> "Eric Brunel" <eric_brunel at despammed.com> wrote in message
> news:opsw2yqkilrqur0o at eb.pragmadev...
>> On Tue, 13 Sep 2005 22:31:31 -0600, Bob Greschke <bob at greschke.com> wrote:
>>
>>> Root.option_add("*?????*font", "Helvetica 12 bold")
>>>
>>> Want to get rid of the "font =":
>>> Widget.add_cascade(label = "File", menu = Fi, font = "Helvetica 12 bold")
>>>
>>> Does anyone know what ????? should be to control the font of the cascade
>>> menus (the labels on the menu bar)?  "*Menu*font" handles the part that
>>> drops down, but I can't come up with the menu bar labels part.
>>
>> option_add('*Menu.font', 'helvetica 12 bold') works for me for all sorts
>> of menu items (cascade, commands, checkbuttons, whatever...).
>>
>> What is your platform? There may be a few limitations for menu fonts on
>> Windows.
>>
> You can set the font for the labels on the menu bar, and you can set the
> font of the items in the drop down portion independently (except on Windows
> where you cannot control the font of the menu bar labels).  I just don't
> know how to set the font for the menu bar labels using an option_add
> command.
>
> ...option_add("*Menu*font", "Helvetica 12 bold")  works fine
> ...option_add("*Cascade*font", "Helvetica 12 bold")  does not,
>
> because I can't figure out what to put in for "Cascade".  There must be
> something, because I can use font = "Helvetica 12 bold" in the add_cascade
> command OK (see below).

I'm still not sure what your exact requirements are. Do you want to have a different font for the menu bar labels and the menu items and to set them via an option_add? If it is what you want, I don't think you can do it: the menus in the menu bar are just ordinary items for tk, and you can't control separately the different kind of menu items. In addition, you can have cascade menus in "regular" menus, like in:

--------------------------------------
 from Tkinter import *

root = Tk()

menuBar = Menu(root)
root.configure(menu=menuBar)

fileMenu = Menu(menuBar)
menuBar.add_cascade(label='File', menu=fileMenu)

openRecentMenu = Menu(fileMenu)
fileMenu.add_cascade(label='Open recent', menu=openRecentMenu)

openRecentMenu.add_command(label='foo.txt')
openRecentMenu.add_command(label='bar.txt')

fileMenu.add_command(label='Quit', command=root.quit)

root.mainloop()
--------------------------------------

All menu items get the options you defined via option_add('*Menu.*', ...), whether they are cascade or commands or whatever. Not doing so would end up in quite weird results.

To do what you seem to want, I'd create a custom class for menu bars like this:

--------------------------------------
 from Tkinter import *

class MyMenuBar(Menu):

   def __init__(self, master, **options):
     Menu.__init__(self, master, **options)
     master.configure(menu=self)

   def add_cascade(self, **options):
     if not options.has_key('font'):
       options['font'] = ('helvetica', 14, 'bold')
     Menu.add_cascade(self, **options)


root = Tk()

menuBar = MyMenuBar(root)

fileMenu = Menu(menuBar)
menuBar.add_cascade(label='File', menu=fileMenu)

openRecentMenu = Menu(fileMenu)
fileMenu.add_cascade(label='Open recent', menu=openRecentMenu)

openRecentMenu.add_command(label='foo.txt')
openRecentMenu.add_command(label='bar.txt')

fileMenu.add_command(label='Quit', command=root.quit)

root.mainloop()
--------------------------------------
This prevents you from putting the font=... option in all menus you create in your menu bar without the need for an option_add.

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"



More information about the Python-list mailing list