Tkinter add_cascade option_add

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Greschke

    #1

    Tkinter add_cascade option_add

    Root.option_add ("*?????*fon t", "Helvetica 12 bold")

    Want to get rid of the "font =":
    Widget.add_casc ade(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


  • Eric Brunel

    #2
    Re: Tkinter add_cascade option_add

    On Tue, 13 Sep 2005 22:31:31 -0600, Bob Greschke <bob@greschke.c om> wrote:
    [color=blue]
    > Root.option_add ("*?????*fon t", "Helvetica 12 bold")
    >
    > Want to get rid of the "font =":
    > Widget.add_casc ade(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.[/color]

    option_add('*Me nu.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
    --
    python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"

    Comment

    • Bob Greschke

      #3
      Re: Tkinter add_cascade option_add

      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

      "Eric Brunel" <eric_brunel@de spammed.com> wrote in message
      news:opsw2yqkil rqur0o@eb.pragm adev...[color=blue]
      > On Tue, 13 Sep 2005 22:31:31 -0600, Bob Greschke <bob@greschke.c om> wrote:
      >[color=green]
      >> Root.option_add ("*?????*fon t", "Helvetica 12 bold")
      >>
      >> Want to get rid of the "font =":
      >> Widget.add_casc ade(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.[/color]
      >
      > option_add('*Me nu.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
      > --
      > python -c "print ''.join([chr(154 - ord(c)) for c in
      > 'U(17zX(%,5.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"[/color]


      Comment

      • Eric Brunel

        #4
        Re: Tkinter add_cascade option_add

        On Wed, 14 Sep 2005 09:58:25 -0600, Bob Greschke <bob@passcal.nm t.edu> wrote:[color=blue]
        > "Eric Brunel" <eric_brunel@de spammed.com> wrote in message
        > news:opsw2yqkil rqur0o@eb.pragm adev...[color=green]
        >> On Tue, 13 Sep 2005 22:31:31 -0600, Bob Greschke <bob@greschke.c om> wrote:
        >>[color=darkred]
        >>> Root.option_add ("*?????*fon t", "Helvetica 12 bold")
        >>>
        >>> Want to get rid of the "font =":
        >>> Widget.add_casc ade(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.[/color]
        >>
        >> option_add('*Me nu.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.
        >>[/color]
        > 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).[/color]

        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_cas cade(label='Fil e', menu=fileMenu)

        openRecentMenu = Menu(fileMenu)
        fileMenu.add_ca scade(label='Op en recent', menu=openRecent Menu)

        openRecentMenu. add_command(lab el='foo.txt')
        openRecentMenu. add_command(lab el='bar.txt')

        fileMenu.add_co mmand(label='Qu it', command=root.qu it)

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

        All menu items get the options you defined via option_add('*Me nu.*', ...), 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__(s elf, master, **options)
        master.configur e(menu=self)

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


        root = Tk()

        menuBar = MyMenuBar(root)

        fileMenu = Menu(menuBar)
        menuBar.add_cas cade(label='Fil e', menu=fileMenu)

        openRecentMenu = Menu(fileMenu)
        fileMenu.add_ca scade(label='Op en recent', menu=openRecent Menu)

        openRecentMenu. add_command(lab el='foo.txt')
        openRecentMenu. add_command(lab el='bar.txt')

        fileMenu.add_co mmand(label='Qu it', command=root.qu it)

        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.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"

        Comment

        • Mikael Olofsson

          #5
          Re: Tkinter add_cascade option_add

          Eric Brunel wrote in reply to Bob Greschke:[color=blue]
          > 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.[/color]

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

          from Tkinter import *
          class App(Tk):
          def __init__(self):
          Tk.__init__(sel f)
          self.option_add ('*Menu.font', 'helvetica 24 bold')
          self.menuBar=Me nu(self)
          self.fileMenu=M enu(self.menuBa r,tearoff=0)
          self.subMenu=Me nu(self.fileMen u)
          self.fileMenu.a dd_cascade(labe l='foo', menu=self.subMe nu)
          self.subMenu.ad d_command(label ='bar',command= self.foo)
          self.subMenu.ad d_command(label ='baz',command= self.foo)
          self.fileMenu.a dd_command(labe l='Quit',comman d=self.quit)
          self.menuBar.ad d_cascade(label ='File',menu=se lf.fileMenu)
          self.config(men u=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.a dd_cascade row, but nothing happens to 'File' if I
          try to specify a font in the self.menuBar.ad d_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

          Comment

          • jepler@unpythonic.net

            #6
            Re: Tkinter add_cascade option_add

            On Thu, Sep 15, 2005 at 06:11:18PM +0200, Mikael Olofsson wrote:[color=blue]
            > 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?[/color]

            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 willbe
            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//Jd01MZaTXX0RAkr zAKCRCltiBUSp/01xaUrcIFr5no5D twCgjPsO
            N+OJJdHvDDDs1c9 1NEKDKBk=
            =kcn+
            -----END PGP SIGNATURE-----

            Comment

            • Bob Greschke

              #7
              Re: Tkinter add_cascade option_add

              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_scre enwidth()
              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*fon t", "Helvetica 10")
              Root.option_add ("*Label*fon t", "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*fon t", "Courier 10")
              Root.option_add ("*Label*fon t", "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*fon t", "Courier 12")
              Root.option_add ("*Label*fon t", "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*takeF ocus", "0")
              Root.option_add ("*Checkbutton* anchor", "w")
              Root.option_add ("*Checkbutton* takeFocus", "0")
              Root.option_add ("*Entry*backgr ound", "white")
              Root.option_add ("*Entry*foregr ound", "black")
              Root.option_add ("*Entry*highli ghtThickness", "2")
              Root.option_add ("*Entry*highli ghtColor", "blue")
              Root.option_add ("*Entry*insert Width", "3")
              Root.option_add ("*Listbox*back ground", "white")
              Root.option_add ("*Listbox*fore ground", "black")
              Root.option_add ("*Listbox*take Focus", "0")
              Root.option_add ("*Radiobutton* takeFocus", "0")
              Root.option_add ("*Scrollbar*ta keFocus", "0")
              Root.option_add ("*Text*backgro und", "black")
              Root.option_add ("*Text*foregro und", "grey")
              Root.option_add ("*Text*takeFoc us", "0")
              Root.option_add ("*Text*widt h", "0")

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

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

              def makeMenu(Win):
              global Di
              Top = Menu(Win)
              Win.configure(m enu = Top)
              Fi = Menu(Top, tearoff = 0)
              Fi.add_command( label = "Erase Messages", command = cmdMsgErase)
              Fi.add_separato r()
              Fi.add_command( label = "Save Parameters", command = parmsSave)
              Fi.add_command( label = "Load Parameters", command = parmsLoad)
              Fi.add_separato r()
              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_separato r()
              Co.add_command( label = "Enter A Message", command = cmdMessage)
              Co.add_separato r()
              Co.add_command( label = "Label/Format USB Drives", \
              command = cmdSetLabelsFor mat)
              Top.add_cascade (label = "Commands", menu = Co, font = CascadeFont)
              Op = Menu(Top, tearoff = 0)
              Op.add_radiobut ton(label = "Set Time From GPS", variable = SetTimeRVar,
              \
              value = "gps", command = Command(subCont , "Rgps,pc", "time", \
              SetTimeRVar))
              Op.add_radiobut ton(label = "Set Time From PC", variable = SetTimeRVar, \
              value = "pc", command = Command(subCont , "Rgps,pc", "time", \
              SetTimeRVar))
              Op.add_separato r()
              Op.add_radiobut ton(label = "Memory Units In KBs", \
              variable = MemUnitsRVar, value = "k")
              Op.add_radiobut ton(label = "Memory Units In Blocks", \
              variable = MemUnitsRVar, value = "blocks")
              Op.add_separato r()
              Op.add_checkbut ton(label = "Wait For USB Drive Responses", \
              variable = DriveRpCVar, command = Command(subCont , "C1,0",
              "drv", \
              DriveRpCVar))
              Op.add_separato r()
              Op.add_checkbut ton(label = "Record Commands To Log", variable = LogCVar,
              \
              command = cmdLog)
              Op.add_separato r()
              Op.add_checkbut ton(label = "Debug Mode", variable = DebugCVar, \
              command = cmdDebug)
              Top.add_cascade (label = "Options", menu = Op, font = CascadeFont)
              Help = Menu(Top, tearoff = 0)
              Help.add_comman d(label = "Reset Commands", command = resetButtons)
              Help.add_comman d(label = "Current Working Directory", command = cmdCWD)
              Help.add_comman d(label = "Serial Port Scan", command = cmdPortScan)
              # TESTING
              # Help.add_comman d(label = "Test", command = cmdTest)
              Help.add_separa tor()
              Help.add_comman d(label = "Help", command = showHelp)
              Help.add_comman d(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


              "Bob Greschke" <bob@greschke.c om> wrote in message
              news:s-KdnUK6pLAuOrreR Vn-uw@nmt.edu...[color=blue]
              > Root.option_add ("*?????*fon t", "Helvetica 12 bold")
              >
              > Want to get rid of the "font =":
              > Widget.add_casc ade(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
              >
              >[/color]


              Comment

              Working...