_________________________________________________________________
menubar .mb -menubuttons {
menubutton file -text File
menubutton edit -text Edit
menubutton options -text Options
}
specifies that three menubuttons will be added to the
menubar (file, edit, options). Each entry is translated
into an add command call.
The menuButtons option can accept embedded variables, commands, and backslash quoting. Embedded variables and commands must be enclosed in curly braces ({}) to ensure proper parsing of the substituted values. _________________________________________________________________
A menubar is a widget that simplifies the task of creating menu hierarchies. It encapsulates a frame widget, as well as menubuttons, menus, and menu entries. The menubar allows menus to be specified and referenced in a more consistent manner than using Tk to build menus directly.
Menubar allows a menu tree to be expressed in a hierachical "language". The menubar accepts a menuButtons option that allows a list of menubuttons to be added to the menubar. In turn, each menubutton accepts a menu option that specifies a list of menu entries to be added to the menubutton's menu. Cascade entries also accept the menu option for specifying a list of menu entries to be added to the cascade's menu.
Additionally, the menubar allows each component of the menubar system to be referenced by a simple menuPathName syntax. The menubar also extends the set of options for menu entries to include a helpStr option.
frame. A menubar holds exactly one frame which manages menubuttons. The frame is always signified by the `.' character as the path name. menubutton. A menubutton corresponds directly to a Tk menubutton. See menubutton(n).
menu. A menu is attached to a menubutton and corresponds directly to Tk's menu widget. A menu is always signified by the menuPathName ending with the keyword menu. See menu(n).
entry. An entry corresponds directly to Tk's menu widget entries. Menus consist of a column of one line entries. Entries may be of type: command, checkbutton, radiobutton, separator, or cascade. For a complete description of these types see the discussion on ENTRIES in menu(n).
The suffix of a menuPathName may have the form of:
tkWidgetName Specifies the name of the component, either a frame, menubutton, menu, or an entry. This is the normal naming of widgets. For example, .file references a menubutton named file.
The menuPathName is a series of segment names, each separated by the `.' character. Segment names may be one of the following forms:
As a restriction, the last name segment of menuPathName cannot be one of the keywords last, menu, end, nor may it be a numeric value (integer).
pathName option ?arg arg ...?
option and the args determine the exact behavior of the command.
In addition, many of the widget commands for menubar take as one argument a path name to a menu component. These path names are called menuPathNames. See the discussion on MENUBAR PATH NAMES above.
The following commands are possible for menubar widgets:
pathName add type menuPathName ?option value option value? Adds either a menu to the menu bar or a menu entry to a menu pane.
If additional arguments are present, they specify options available to component type entry. See the man pages for menu(1) in the section on ENTRIES.
If type is one of cascade, checkbutton, command, radiobutton, or separator it adds a new entry to the bottom of the menu denoted by the prefix of menuPath_Name. If additonal arguments are present, they specify options available to menu entry widgets. In addition, the helpStr option is added by the menubar widget to all components of type entry.
Note that the same curly brace quoting rules apply to -menu option strings as did to -menubuttons option strings. See the earlier discussion on umenubuttons in the "WIDGET-SPECIFIC OPTIONS" section.
pathName cget option
Returns the current value of the configuration option
given by option.
pathName configure ?options value option value? Query or modify the configuration options of the widget. If no option is specified, returns a list describing all of the available options for pathName (see Tk_ConfigureInfo for information on the format of this list). If option is specified with no value, then the command returns a list describing the one named option (this list will be identical to the corresponding sublist of the value returned if no option is specified). If one or more option-value pairs are specified, then the command modifies the given widget option(s) to have the given value(s); in this case the command returns an empty string.
pathName delete menuPathName ?menuPathName2? If menuPathName is of component type Menubutton or
Menu, delete operates on menus. If menuPathName is of component type Entry, delete operates on menu entries.
This command deletes all components between menuPath_Name and menuPathName2 inclusive. If menuPathName2 is omitted then it defaults to menuPathName. Returns an empty string.
If menuPathName is of type menubar, then all menus and the menu bar frame will be destroyed. In this case menuPathName2 is ignored.
pathName index menuPathName
If menuPathName is of type menubutton or menu, it
returns the position of the menu/menubutton on the
menubar frame.
If menuPathName is of type command, separator, radiobutton, checkbutton, or cascade, it returns the menu widget's numerical index for the entry corresponding to menuPathName. If path is not found or the path is equal to ".", a value of -1 is returned.
pathName insert menuPathName type name ?option value? Insert a new component named name before the component specified by menuPathName.
If menuPathName is of type Menubutton or Menu, the new component inserted is of type Menu and given the name name. In this case valid option value pairs are those accepted by menubuttons.
If menuPathName is of type Entry, the new component inserted is of type entry and given the name name. In this case, valid option value pairs are those accepted by menu entries. Name cannot be one of the keywords last, menu, end. Additionally, it may not be a number. However the menuPathName may be referenced in this manner (see discussion of COMPONENT PATH NAMES).
pathName invoke menuPathName
Invoke the action of the menu entry denoted by menu_PathName.
See the sections on the individual entries in
the menu(1) man pages. If the menu entry is disabled
then nothing happens. If the entry has a command associated
with it then the result of that command is
returned as the result of the invoke widget command.
Otherwise the result is an empty string.
If menuPathName is not a menu entry, an error is issued.
pathName menucget menuPathName option
Returns the current value of the configuration option
given by option. The component type of menuPathName
determines the valid available options.
pathName menuconfigure menuPathName ?option value? Query or modify the configuration options of the componet of the menubar specified by menuPathName. If no option is specified, returns a list describing all of the available options for menuPathName (see Tk_ConfigureInfo for information on the format of this list). If option is specified with no value, then the command returns a list describing the one named option (this list will be identical to the corresponding sublist of the value returned if no option is specified). If one or more option-value pairs are specified, then the command modifies the given widget option(s) to have the given value(s); in this case the command returns an empty string. The component type of menuPathName determines the valid available options.
pathName path ?mode? pattern
Returns a fully formed menuPathName that matches pat_tern.
If no match is found it returns -1. The mode
argument indicates how the search is to be matched
against pattern and it must have one of the following
values:
pathName yposition menuPathName
Returns a decimal string giving the y-coordinate within
the menu window of the topmost pixel in the entry
specified by menuPathName. If the menuPathName is not
an entry, an error is issued.
menubar .mb -helpvariable helpVar -menubuttons {
menubutton file -text File -menu {
options -tearoff false
command new -label New \
-helpstr "Open new document" \
-command {puts NEW}
command close -label Close \
-helpstr "Close current document" \
-command {puts CLOSE}
separator sep1
command exit -label Exit -command {exit} \
-helpstr "Exit application"
}
menubutton edit -text Edit -menu {
options -tearoff false
command undo -label Undo -underline 0 \
-helpstr "Undo last command" \
-command {puts UNDO}
separator sep2
command cut -label Cut -underline 1 \
-helpstr "Cut selection to clipboard" \
-command {puts CUT}
command copy -label Copy -underline 1 \
-helpstr "Copy selection to clipboard" \
-command {puts COPY}
command paste -label Paste -underline 0 \
-helpstr "Paste clipboard contents" \
-command {puts PASTE}
}
menubutton options -text Options -menu {
options -tearoff false -selectcolor blue
radiobutton byName -variable viewMode \
-value NAME -label "by Name" \
-helpstr "View files by name order" \
-command {puts NAME}
radiobutton byDate -variable viewMode \
-value DATE -label "by Date" \
-helpstr "View files by date order" \
-command {puts DATE}
cascade prefs -label Preferences -menu {
command colors -label Colors... \
-helpstr "Change text colors" \
-command {puts COLORS}
command fonts -label Fonts... \
-helpstr "Change text font" \
-command {puts FONT}
}
}
}
frame .fr -width 300 -height 300
entry .ef -textvariable helpVar
pack .mb -anchor nw -fill x -expand yes
pack .fr -fill both -expand yes
pack .ef -anchor sw -fill x -expand yes
menubar .mb
.mb configure -menubuttons {
menubutton file -text File -menu {
command new -label New
command close -label Close
separator sep1
set fileMenuName "File Menu"
set var {}
menubar .mb -menubuttons {
menubutton file -text {$fileMenuName}
menubutton edit -text Edit -menu {
checkbutton check \
-label Check \
-variable {[scope var]} \
-onvalue 1 \
-offvalue 0
}
menubutton options -text Options
}
The variable fileMenuName will expand to "File Menu" when the subst command is used on the menubutton specification. In addition, the [scope...] command will expand to @scope :: var. By enclosing these inside {} they stay as a single value. Note that only {} work for this. [list...], "" etc. will not protect these from the subst command.
1994 - Early work on a menubar widget.
Mark Ulferts, Mark Harrison, John Sigler
Invaluable feedback on grammar and usability of the menubar widget