Programming |  Tck/Tk Programming |  Tk Widgets |  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 NAME Specifies the path name of the menu associated with this menubutton. The menu must be a child of the menubutton.

    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

  • 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.