Creating a menu

In Tk the menu is not just a name for a part of your GUI, but also the actual widget command name as well. The menu command will create a new menu widget.

The syntax is as follows:

	menu name option value…

The menu command accepts one or more option value pairs, as detailed in the following table:

Option

Interpretation

-accelerator

Specifies an accelerator or keyboard hotkeys to be displayed to the right-hand side of the menu text. The specific acceptable values are dependent on the display manager in use. For example, in a Windows application, Control+N would be an acceptable value.

-activebackground

Specifies the background color to be used when drawing the element. The active background is the color used when the mouse is over the element and when pressing the mouse button will initiate an action.

-activeborderwidth

Specifies the width of the 3D border to draw around the active item.

-activeforeground

Specifies the foreground color to be used when drawing the element. The active foreground is the color used when the mouse is over the element and when pressing the mouse button will initiate an action.

-background or –bg

Specifies the background color to be used when drawing the element.

-borderwidth or -bd

Specifies a non-negative value indicating the width of the 3D border to draw around the outside of the window.

-cursor

Specifies the mouse cursor to be used for the window.

-disabledforeground

Specifies the color to use when displaying a disabled element.

-font

Specifies the font to use when drawing the element

-foreground or –fg

Specifies the normal foreground color to be used when drawing the element.

-relief

Specifies the 3D effect desired for the window. Acceptable values are raised, sunken, flat, ridge, solid, and groove.

-takefocus

Determines whether or not the window accepts the focus during keyboard traversal.

-postcommand

If specified, this provides a command to execute each time the menu is posted.

-selectcolor

Specifies the color to display as the background for menus containing check or radio buttons when they are selected.

-tearoff

This option accepts a Boolean value and specifies if a menu should include a tear-off entry. A tearoff entry allows the user to detach the menu item and display it independent of the menu.

-tearoffcommand

If specified, this provides a command to execute each time the menu is torn off.

-title

Specifies the title to display for the window created if a menu is torn off. If not specified, the window title will display the label for the top level menu item.

-type

Specifies the type of menu. Acceptable values are menubar, tearoff, or normal..

Creation of the menu widget additionally created a new Tcl command of the same name. These commands are accessed using the following syntax:

	name command arguments

Several of the menu commands accept an argument to indicate which menu entry to affect. These are referred to as the indexes and may be specified in any of the following manners. Note that those items displayed in italics are not keywords but indicate a specific numeric or textual value placeholder.

Index

Interpretation

number

A numeric designation of the index with a base of 0.

active

The menu item that is currently active.

end

Last entry in a menu.

last

Last entry in a menu.

none

Normally used with the activate command, this is used to deactivate all menu items.

@number

When utilized in this manner, the number is treated as a y-coordinate and the entry closest to the coordinate is used.

pattern

Used to perform pattern matching on the label of each entry when none of the above index methods is sufficient.

The commands are as follows:

Specific commands

Interpretation

name activate index

Set the menu item at index to activated.

name add type option value…

This command adds a new menu item at the bottom of the menu. The type of entry is specified by type. The acceptable values for type are cascade, checkbutton, command, radiobutton, or separator. Additional arguments are specified as an option/value pair, as detailed below:

-activebackground value: Specifies a background color to display when the item is active.

-activeforeground value: Specifies a foreground color to display when the item is active.

-accelerator value: Specifies a string to display at the right side of the menu item. Normally, used to display a keyboard shortcut. This is not available for separator or tearoff items.

-background value: Specifies a background color to display when the item is in a normal state.

-bitmap value: Specifies a bitmap to display in the menu instead of a textual label. This is not available for separator or tearoff items.

-columnbreak value: When set to 0, the entry appears below the previous entry. When set to 1, the item appears at the top of a new column.

-command value: Specifies a Tcl command to execute when the menu item is activated.

-compound value: Specifies if the menu should display both an image and text and where the image should be displayed. Acceptable values are bottom, center, left, none (default), right, or top.

-font value: Specifies the font to use when displaying the item.

-foreground value: Specifies the foreground color to use for displaying the menu item.

-hidemargin value: Specifies whether or not the standard margins should be drawn for this menu. The 0 indicates that the margin is used, 1 indicates that it is not.

-image value: Specifies an image to display in place of a bitmap or textual label. This is not available for separator or tearoff items.

-indicatoron value: Only applies to checkbox or radio button items. Accepts a Boolean value.

 

-label value: Specifies a string to display for the item. This is not available for separator or tearoff items.

-menu value: Cascade items only. Specifies the name for the submenus associated with this item.

-offvalue value: Check button only. Specifies the value to store in the item's associated variable when the entry is deselected.

-onvalue value: Check button only. Specifies the value to store in the item's associated variable when the entry is deselected.

-selectcolor value: Check button and radio button only. Specifies the color to display when the item is selected.

-selectimage value: Check button and radio button only. Specifies an image to display in place of the default image when the item is selected.

-state value: Specifies the state of the item. Acceptable values are normal, active or disabled. This is not available for separator items.

-underline value: Specifies the index of a character in the label to underline as a keyboard accelerator. This index is 0 based and is not available for separator or tearoff items.

-value value: Radio button only. Specifies the value to store in the item's variable when the item is selected.

-variable value: Check button and radio button only. Specifies the name of a global variable to store the value for the item.

name cget option

Returns the current configuration value for the option specified.

name clone newname type

Create a clone of the menu with the name as specified in newname of the type normal, menubar, or tearoff. Changes to one menu propagate to the other and are bidirectional.

name configure option value…

Query or modify the option of the menu.

name delete index1 index2

Delete all menu items between index1 and index2 inclusive. If no value is passed for index2, only the item at index1 is deleted.

name entrycget index option

Returns the current configuration value for the option specified for the item at index.

name entryconfigure index options

Query or modify the option of the menu item specified at index.

name index index

Returns the index for the corresponding index.

name insert index type option value…

This is the same as the add command except it inserts a new item after the item located at index.

name invoke index

Invoke the action for the item specified by index.

name post x y

Display the menu at the coordinates provided by x and y.

name postcascade index

Display the submenu associated with the cascade item specified by index and unpost any previously displayed submenus.

name type index

Returns the type of item as specified by index.

name unpost

Remove the menu from display. Not available on Windows or Macintosh.

name xposition index

Returns the x-coordinate of the leftmost pixel in the item specified in index.

name yposition index

Returns the y-coordinate of the leftmost pixel in the item specified in index.

How to do it…

In the following example, we will create a menu that contains an option to exit the application. Create the following text file and save it in your working path with the name my_menu.tcl:

# Load the TK Package
package require Tk
#Define our interface
wm geometry . 320x240
wm title . "Menu Example"
# Create a menu to exit our application
menu .myMenu
.configure -menu .myMenu
# Add a pull down
set File [menu .myMenu.myfile]
.myMenu add cascade -label File -menu .myMenu.myfile
# Add the Exit entry
$File add command -label Exit -command exit

Now launch the program by invoking the following command line command.


tclsh85 my_menu.tcl

You should now see the following window:

How to do it…

Click on the File menu item to display the Exit option. Select this option to exit the window.

How it works…

Based on the configuration options provided to the menu command we have created a menu with an entry to exit our program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.82.154