programming.nbk: Home | Index | Next Page: Tk: radiobutton | Previous Page: Tk: label


 Tk: menus

menubutton

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:

menu

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:

Some Commands

Syntax Description

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

programming.nbk: Home | Index | Next Page: Tk: radiobutton | Previous Page: Tk: label


Notebook exported on Monday, 7 July 2008, 18:56:06 PM Eastern Daylight Time