Previous
Listbox 
Next
Canvas, Message 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Menubutton, Menu

Widgets 5 : Menubutton, Menu, tk_optionMenu

A menubutton is a widget that displays a textual string, bitmap, or image and is associated with a menu widget.In normal usage, pressing left-clicking the menubutton causes the associated menu to be posted just underneath the menubutton.

Some Options
-direction DIRECTION Specifies where the menu is going to be popup up. above tries to pop the menu above the menubutton. below tries to pop the menu below the menubutton. left tries to pop the menu to the left of the menubutton. right tries to pop the menu to the right of the menu button. flush pops the menu directly over the menubutton.
-menu NAME Specifies the path name of the menu associated with this menubutton. The menu must be a child of the menubutton.

A menu is a widget that displays a collection of one-line entries arranged in one or more columns. There exist several different types of entries, each with different properties. Entries of different types may be combined in a single menu. Menu entries are not the same as entry widgets. In fact, menu entries are not even distinct widgets; the entire menu is one widget.

Some Options
-tearoff BOOLEAN This option must have a proper boolean value, which specifies whether or not the menu should include a tear-off entry at the top. If so, it will exist as entry 0 of the menu and the other entries will number starting at 1. The default menu bindings arrange for the menu to be torn off when the tear-off entry is invoked.
-title STRING The string will be used to title the window created when this menu is torn off. If the title is NULL, then the window will have the title of the menubutton or the text of the cascade item from which this menu was invoked.
-type OPTION This option can be one of menubar, tearoff, or normal, and is set when the menu is created. While the string returned by the configuration database will change if this option is changed, this does not affect the menu widget's behavior.


Some Commands
SyntaxDescription
path add type ?option
value option value ...?
Add a new entry to the bottom of the menu. The new entry's type is given by type and must be one of cascade, checkbutton, command, radiobutton, or separator, or a unique abbreviation of one of the above. If additional arguments are present, they specify any of the following options:
-accelerator VALUE Specifies a string to display at the right side of the menu entry. Normally describes an accelerator keystroke sequence that may be typed to invoke the same function as the menu entry. This option is not available for separator or tear-off entries.
-columnbreak VALUE When this option is zero, the appears below the previous entry. When this option is one, the menu appears at the top of a new column in the menu
-label VALUE Specifies a string to display as an identifying label in the menu entry. Not available for separator or tear-off entries.
-compound VALUE Specifies whether the menu entry should display both an image and text, and if so, where the image should be placed relative to the text. Valid values for this option are bottom, center, left, none, right and top.
-image VALUE Specifies an image to display in the menu instead of a text string or bitmap The image must have been created by some previous invocation of image create. This option overrides the -label and -bitmap options but may be reset to an empty string to enable a textual or bitmap label to be displayed. This option is not available for separator or tear-off entries.
-underline VALUE Specifies the integer index of a character to underline in the entry. This option is used to make keyboard shortcuts. 0 corresponds to the first character of the text displayed in the entry, 1 to the next character, and so on.
path clone newPathname Makes a clone of the current menu named newPathName. This clone is a menu in its own right, but any changes to the clone are propagated to the original menu and vice versa.
path delete index1
?index2?
Delete all of the menu entries between index1 and index2 inclusive. If index2 is omitted then it defaults to index1. Attempts to delete a tear-off menu entry are ignored (instead, you should change the tearOff option to remove the tear-off entry).
path insert index type
?option value ...?
Same as the add widget command except that it inserts the new entry just before the entry given by index, instead of appending to the end of the menu. The type, option, and value arguments have the same interpretation as for the add widget command. It is not possible to insert new menu entries before the tear-off entry, if the menu has one.


Example

proc menu_clicked { no opt } {
	tk_messageBox -message \
		"You have clicked $opt.\nThis function is not implanted yet."
}

#Declare that there is a menu
menu .mbar
. config -menu .mbar

#The Main Buttons
.mbar add cascade -label "File" -underline 0 \
      -menu [menu .mbar.file -tearoff 0]
.mbar add cascade -label "Others" \
      -underline 0 -menu [menu .mbar.oth -tearoff 1]
.mbar add cascade -label "Help" -underline 0 \
      -menu [menu .mbar.help -tearoff 0]

## File Menu ##
set m .mbar.file
$m add command -label "New" -underline 0 \
	  -command { .txt delete 1.0 end } ;# A new item called New is added.
$m add checkbutton -label "Open" -underline 0 -command { menu_clicked 1 "Open" }
$m add command -label "Save" -underline 0 -command { menu_clicked 1 "Save" }
$m add separator
$m add command -label "Exit" -underline 1 -command exit

## Others Menu ##
set m .mbar.oth
$m add cascade -label "Insert" -underline 0 -menu [menu $m.mnu -title "Insert"] 
  $m.mnu add command -label "Name" \
       -command { .txt insert end "Name : Binny V A\n"}
  $m.mnu add command -label "Website" -command { \
     .txt insert end "Website: http://www.bin-co.com/\n"}
  $m.mnu add command -label "Email" \
	   -command { .txt insert end "E-Mail : binnyva@hotmail.com\n"}
$m add command -label "Insert All" -underline 7 \
	-command { .txt insert end {Name : Binny V A
	  Website : http://www.bin-co.com/
	  E-Mail : binnyva@hotmail.com}
	  }

## Help ##
set m .mbar.help
$m add command -label "About" -command { 
	.txt delete 1.0 end
	.txt insert end {
	About
	----------
	This script created to make a menu for a tcl/tk tutorial.
	Made by Binny V A
	Website : http://www.bin-co.com/
	E-Mail : binnyva@hotmail.com
	}
	}

#Making a text area
text .txt -width 50
pack .txt

Create the main buttons as cascade menus and create the menus as their slaves. For more information see the manual.


tk_optionMenu

Makes a button, which when clicked on shows a list with available options. Useful when user has to make one choice when multiple choices are given. Below is a options menu in HTML. A word of caution though - Tcl/Tk's option menu has a very different appearance.

Syntax
tk_optionMenu path varName value ?value value ...?

Example...
set opt "Two"
tk_optionMenu .omn opt "One" "Two" "Three" "Four" "Five" "etc."
pack .omn
Previous
Listbox 
Next
Canvas, Message 
Subscribe to Feed