Tkinter add_cascade option_add

B

Bob Greschke

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.

Thanks!

Bob
 
E

Eric Brunel

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.

HTH
 
B

Bob Greschke

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).

The program runs on Windows, Linux, Solaris, Mac.

Bob
 
E

Eric Brunel

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()
 
M

Mikael Olofsson

Eric Brunel wrote in reply to Bob Greschke:
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.

I guess he has the same problem as I have. See sample code:

from Tkinter import *
class App(Tk):
def __init__(self):
Tk.__init__(self)
self.option_add('*Menu.font', 'helvetica 24 bold')
self.menuBar=Menu(self)
self.fileMenu=Menu(self.menuBar,tearoff=0)
self.subMenu=Menu(self.fileMenu)
self.fileMenu.add_cascade(label='foo', menu=self.subMenu)
self.subMenu.add_command(label='bar',command=self.foo)
self.subMenu.add_command(label='baz',command=self.foo)
self.fileMenu.add_command(label='Quit',command=self.quit)
self.menuBar.add_cascade(label='File',menu=self.fileMenu)
self.config(menu=self.menuBar)
def foo(self):
pass
app=App()
app.mainloop()

What happens on my WinXP-box when I run this code is that the menu bar
still is displayed in the standard font, which seems to be Helvetica 8.
I.e. The text 'File' is still small, while everything else in the menus
is displayed in Helvetica 24 bold. Adding font=... to any of the
commands above does not change the appearance of 'File'. I have no
problems changing the appearance of any individual text in the menu
except for the mentioned 'File'. I can change the appearance of 'foo' in
the self.fileMenu.add_cascade row, but nothing happens to 'File' if I
try to specify a font in the self.menuBar.add_cascade row. Doesn't that
seem a bit peculiar?

But... The example you gave does not change the appearance of the menus
at all on my machine. I guess it was supposed to?

Is this simply a Windows-issue that cannot easily be solved? Or is it
possibly so that this just happens to be a problem on a few
ill-configured computers? Or am I possibly blind?

/MiO
 
J

jepler

Is this simply a Windows-issue that cannot easily be solved? Or is it
possibly so that this just happens to be a problem on a few
ill-configured computers? Or am I possibly blind?

Here's a section from the menu(n) manpage for Tcl.

It would appear that the font for the menubar is one of the things which
happens 'according to the interface guidelines of their platforms' on
win32-family systems.

MENUBARS
Any menu can be set as a menubar for a toplevel window (see toplevel
command for syntax). On the Macintosh, whenever the toplevel is in
front, this menu's cascade items will appear in the menubar across the
top of the main monitor. On Windows and Unix, this menu's items will be
displayed in a menubar accross the top of the window. These menus will
behave according to the interface guidelines of their platforms. For
every menu set as a menubar, a clone menu is made. See the CLONES sec-
tion for more information.

As noted, menubars may behave differently on different platforms. One
example of this concerns the handling of checkbuttons and radiobuttons
within the menu. While it is permitted to put these menu elements on
menubars, they may not be drawn with indicators on some platforms, due
to system restrictions.

On Windows, the font used for the menu can be ajusted by the user for all
applications (except those which go out of their way to ignore the user's
preferences) using the Display item in Control Panel.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFDKZ//Jd01MZaTXX0RAkrzAKCRCltiBUSp/01xaUrcIFr5no5DtwCgjPsO
N+OJJdHvDDDs1c91NEKDKBk=
=kcn+
-----END PGP SIGNATURE-----
 
B

Bob Greschke

OK. I give. Here's the whole reason I'm trying to do this.

At the top of the program:
--------------------------

# Fonts: a constant nagging problem

System = sys.platform[:3].lower()
ScreenWidth = Root.winfo_screenwidth()
if System == "win":
if ScreenWidth <= 1024:
Root.option_add("*Button*font", "Helvetica 8 bold")
Root.option_add("*Menu*font", "Helvetica 8 bold")
CascadeFont = "Helvetica 8 bold"
Root.option_add("*Text*font", "Courier 10")
elif ScreenWidth <= 1400:
Root.option_add("*Button*font", "Helvetica 12 bold")
Root.option_add("*Checkbutton*font", "Helvetica 12")
Root.option_add("*Entry*font", "Helvetica 10")
Root.option_add("*Label*font", "Helvetica 12")
Root.option_add("*Menu*font", "Helvetica 12 bold")
CascadeFont = "Helvetica 12 bold"
Root.option_add("*Radiobutton*font", "Helvetica 12")
Root.option_add("*Text*font", "Courier 12")
elif System == "sun":
Root.option_add("*Button*font", "Helvetica 10 bold")
Root.option_add("*Checkbutton*font", "Helvetica 10")
Root.option_add("*Entry*font", "Courier 10")
Root.option_add("*Label*font", "Helvetica 10")
Root.option_add("*Listbox*font", "Courier 10")
Root.option_add("*Menu*font", "Helvetica 10 bold")
CascadeFont = "Helvetica 10 bold"
Root.option_add("*Radiobutton*font", "Helvetica 10")
Root.option_add("*Text*font", "Courier 11")
elif System == "lin":
Root.option_add("*Button*font", "Helvetica 12 bold")
Root.option_add("*Checkbutton*font", "Helvetica 12")
Root.option_add("*Entry*font", "Courier 12")
Root.option_add("*Label*font", "Helvetica 12")
Root.option_add("*Listbox*font", "Courier 12")
Root.option_add("*Menu*font", "Helvetica 12 bold")
CascadeFont = "Helvetica 12 bold"
Root.option_add("*Radiobutton*font", "Helvetica 12")
Root.option_add("*Text*font", "Courier 12")
Root.option_add("*Button*takeFocus", "0")
Root.option_add("*Checkbutton*anchor", "w")
Root.option_add("*Checkbutton*takeFocus", "0")
Root.option_add("*Entry*background", "white")
Root.option_add("*Entry*foreground", "black")
Root.option_add("*Entry*highlightThickness", "2")
Root.option_add("*Entry*highlightColor", "blue")
Root.option_add("*Entry*insertWidth", "3")
Root.option_add("*Listbox*background", "white")
Root.option_add("*Listbox*foreground", "black")
Root.option_add("*Listbox*takeFocus", "0")
Root.option_add("*Radiobutton*takeFocus", "0")
Root.option_add("*Scrollbar*takeFocus", "0")
Root.option_add("*Text*background", "black")
Root.option_add("*Text*foreground", "grey")
Root.option_add("*Text*takeFocus", "0")
Root.option_add("*Text*width", "0")

Then in another section:
------------------------

######################
# BEGIN: makeMenu(Win)
DebugCVar = IntVar()
LogCVar = IntVar()
LogFp = None
MsgFp = None
SetTimeRVar = StringVar()
SetTimeRVar.set("gps")
MemUnitsRVar = StringVar()
MemUnitsRVar.set("k")
DriveRpCVar = IntVar()
DriveRpCVar.set(1)

def makeMenu(Win):
global Di
Top = Menu(Win)
Win.configure(menu = Top)
Fi = Menu(Top, tearoff = 0)
Fi.add_command(label = "Erase Messages", command = cmdMsgErase)
Fi.add_separator()
Fi.add_command(label = "Save Parameters", command = parmsSave)
Fi.add_command(label = "Load Parameters", command = parmsLoad)
Fi.add_separator()
Fi.add_command(label = "Quit", command = progQuitter)
Top.add_cascade(label = "File", menu = Fi, font = CascadeFont)
Co = Menu(Top, tearoff = 0)
Co.add_command(label = "Get Event Table", command = cmdGetET)
Co.add_command(label = "Manual VCXO", command = cmdMVCXOForm)
Co.add_separator()
Co.add_command(label = "Enter A Message", command = cmdMessage)
Co.add_separator()
Co.add_command(label = "Label/Format USB Drives", \
command = cmdSetLabelsFormat)
Top.add_cascade(label = "Commands", menu = Co, font = CascadeFont)
Op = Menu(Top, tearoff = 0)
Op.add_radiobutton(label = "Set Time From GPS", variable = SetTimeRVar,
\
value = "gps", command = Command(subCont, "Rgps,pc", "time", \
SetTimeRVar))
Op.add_radiobutton(label = "Set Time From PC", variable = SetTimeRVar, \
value = "pc", command = Command(subCont, "Rgps,pc", "time", \
SetTimeRVar))
Op.add_separator()
Op.add_radiobutton(label = "Memory Units In KBs", \
variable = MemUnitsRVar, value = "k")
Op.add_radiobutton(label = "Memory Units In Blocks", \
variable = MemUnitsRVar, value = "blocks")
Op.add_separator()
Op.add_checkbutton(label = "Wait For USB Drive Responses", \
variable = DriveRpCVar, command = Command(subCont, "C1,0",
"drv", \
DriveRpCVar))
Op.add_separator()
Op.add_checkbutton(label = "Record Commands To Log", variable = LogCVar,
\
command = cmdLog)
Op.add_separator()
Op.add_checkbutton(label = "Debug Mode", variable = DebugCVar, \
command = cmdDebug)
Top.add_cascade(label = "Options", menu = Op, font = CascadeFont)
Help = Menu(Top, tearoff = 0)
Help.add_command(label = "Reset Commands", command = resetButtons)
Help.add_command(label = "Current Working Directory", command = cmdCWD)
Help.add_command(label = "Serial Port Scan", command = cmdPortScan)
# TESTING
# Help.add_command(label = "Test", command = cmdTest)
Help.add_separator()
Help.add_command(label = "Help", command = showHelp)
Help.add_command(label = "About", command = about)
Top.add_cascade(label = "Help", menu = Help, font = CascadeFont)
return
# END: makeMenu

Do you see the CascadeFont variable? I want to get rid of that and turn
setting the font of the .add_cascade items into an option_add function.
Does anyone know how to do that? What key word and tricky phrase does the
option_add function need?

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top