Version 21 of menu

Updated 2009-03-10 13:17:11 by suchenwi

The documentation for menu and tk_menuSetFocus can be found at http://www.purl.org/tcl/home/man/tcl8.4/TkCmd/menu.htm .


Popup Menus See tk_popup and popup.


Contents:

  • How to add to the System menu on Windows
  • How to add control of the console to the System menu

See also Multilingual menu - Menus made easy - Most Recently Used (MRU) Menus - Visual menus - m+ - Tearoff


On Win XP Classic at least, RS gets a nice groove below the menu with

    pack [frame .sep -relief sunken -borderwidth 1 -height 2] -side top -fill x 

How to add to the System menu in windows: Courtesy KBK (Kevin Kenny) on the comp.lang.tcl newsgroup: Chris Nelson wrote: I'm trying to add an item to the system menu of an NT-based Tcl application and nothing is happening. What am I doing wrong?

 % menu .mb
 .mb
 % . configure -menu .mb
 % .mb add command -label "file"
 % menu .mb.system
 .mb.system
 % .mb.system add  command -label "foo"

This confused me for a while, too. You also need to do:

    .mb add cascade -label System -menu .mb.system

Also, tearoffs look awful in the system menu. You probably want to do:

    .mb.system configure -tearoff 0

NEM (19Jan04) -- In order to add items to the application/system menu under Mac OS X (where a lot of apps put an "About..." and "Preferences.." menu items, you seem to have to create a menu with the label "Apple" (and possibly with the a path ending in .apple, I'm not really sure which bit gets it to work:

 menu .mb
 . configure -menu .mb
 menu .mb.apple -tearoff 0
 .mb.apple add command -label "About My App.."
 .mb add cascade -label "Apple" -menu .mb.apple

KBK: (14 November 2000) -- After I posted this, I went to put it on the Wiki, and found that someone already had! (I guess the Wiki runs on Internet time, too!) Well, I can improve on my own work with this little convenience:


How to add console control to the System menu on Windows:

I've found it useful to be able to do 'console hide' and 'console show' in applications that are already deployed, and found that the system menu is one place to put the function that doesn't clutter up the user interface. (A View menu, if the app has one, might also be a logical choice.)

As soon as she saw it, my system tester said that the separate Show and Hide functions ought to be a checkbutton. I found that required a little bit more hacking, and this was the result:

    # The ''consoleAux'' namespace holds variables and procedures
    # that help manage the system console on Windows

    namespace eval consoleAux {
        namespace export setup;         # Initialization
        namespace export toggle;        # Command to map/unmap the
                                        # console on demand
        variable mapped;                # Flag == 1 iff the console
                                        # is currently mapped
    }
    #------------------------------------------------------------------
    #
    # consoleAux::setup --
    #
    #       Set up the system console control on Windows.
    # 
    # Parameters:
    #       None.
    #
    # Results:
    #       None.
    #
    # Side effects:
    #       Bindings are established so that the variable,
    #       '::consoleAux::mapped' is set to reflect the state
    #       of the console.
    #
    # Notes:
    #       Depends on undocumented internal API's of Tk and
    #       therefore may not work on future releases.
    #
    #------------------------------------------------------------------

    proc consoleAux::setup {} {

        # Make the console have a sensible title
        console title "Console: [tk appname]"
        console eval {

            # Determine whether the console has started in the
            # mapped state.

            if { [winfo ismapped .console] } {
                consoleinterp eval {
                    set ::consoleAux::mapped 1
                }
            } else {
                consoleinterp eval {
                    set ::consoleAux::mapped 0
                }
            }
            # Establish bindings to reflect the state of the
            # console in the 'mapped' variable.

            bind .console <Map> {
                consoleinterp eval {
                    set ::consoleAux::mapped 1
                }
            }
            bind .console <Unmap> {
                consoleinterp eval {
                    set ::consoleAux::mapped 0
                }
            }
        }
        return
    }
    #------------------------------------------------------------------
    #
    # consoleAux::toggle --
    #
    #       Change the 'mapped' state of the console in response
    #       to a checkbutton.
    #
    # Parameters:
    #       None.
    #
    # Results:
    #       None.
    #
    # Side effects:
    #       If the console is marked 'mapped', shows and raises it.
    #       Otherwise, hides it.
    #
    #------------------------------------------------------------------

    proc consoleAux::toggle {} {
        variable mapped
        if {$mapped} {
            console show
            console eval { raise . }
        } else {
            console hide
        }
        return
    }
    # Sample application that shows the use of the console in the
    # system menu.

    # First, make sure there is a system menu.

    menu .menubar
    . configure -menu .menubar

    .menubar add cascade -label File -menu .menubar.file -underline 0

    menu .menubar.file
    .menubar.file add command -label Exit -underline 1 -command exit

    if { [string equal $tcl_platform(platform) windows] } {
        .menubar add cascade -label System -menu .menubar.system

        menu .menubar.system -tearoff 0

        # Add the 'Show Console' item to the system menu

        ::consoleAux::setup
        .menubar.system add checkbutton \
            -label {Show Console} \
            -variable ::consoleAux::mapped \
            -command ::consoleAux::toggle
    }

CT: (26 May 2003) -- I seem to be having problems with compound popup entries under windows. I am using both image and text labels for menu entries in popups and am experiencing problems with the images. Firstly the images only half appear in the menu until you hover over the menu item and it highlights - then the image displays properly. Secondly images with transparency in them are even worse and seem to cause a kind of black smudge obscurring half the image and only correctly display when you hover over them. I am loading regular gifs created in gimp using the image create photo ... command. Problem seems to exist in 98 and XP and its driving me potty...arg! :) Any help would be warmly appreciated, thanks.

MAK (Hm, still 25 May 2003 here :P) regarding the images that only half-appear until you hover: this is a common annoyance with checkbutton menus as well. It has something to do with a somewhat long-standing incompatibility between Tk and Windows' animated menus/windows. If you turn off animated menus/windows then the problem goes away. It may help with your transparent images, too. How you do this depends on the version of Windows, I believe. Would be nice if this were fixed some day, though.

CT: Thanks Mak, this one has been bugging me for years and I could only manage to ignore it for a few months at a time. :)


MG Jan 26 2006 - I've seen menus in Windows (notably in MS Word) which contain entry widgets - when you're configurable a toolbar, you can right-click a button, and in the menu that pops up there's an entry-style widget where you can type in a name for the button. Is there any way to do this in Tk? And if not, is there ever likely to be? I'm not sure if it's a native function of Windows menus which Tk simply doesn't make use of, or whether it's a bit of clever trickery specific to MS Word.

DKF 24 Aug 2006: I think it's an MSOffice thing. It's not the first (or last) time that MS's Office people have been off doing something very odd behind the backs of the general Windows UI people. But as such, it's all done with lots of code that we don't provide access to.



[Category Widget|Category Command|Tk syntax help]