Next / Previous / Contents / TCC Help System / NM Tech homepage

13. The Menubutton widget

A menubutton is the part of a drop-down menu that stays on the screen all the time. Every menubutton is associated with a Menu widget (see above) that can display the choices for that menubutton when the user clicks on it.

To create a menubutton within a root window or frame master:

    w = Menubutton ( master, option, ... )

The constructor returns the new menubutton widget. Options:

activebackgroundThe background color when the mouse is over the menubutton. See Section 4.3, “Colors”.
activeforegroundThe foreground color when the mouse is over the menubutton.
anchorThis options controls where the text is positioned if the widget has more space than the text needs. The default is anchor=CENTER, which centers the text. For other options, see Section 4.5, “Anchors”. For example, if you use anchor=W, the text would be centered against the left side of the widget.
bg or backgroundThe background color when the mouse is not over the menubutton.
bitmapTo display a bitmap on the menubutton, set this option to a bitmap name; see Section 4.7, “Bitmaps”.
bd or borderwidthWidth of the border around the menubutton. Default is 2 pixels. For possible values, see Section 4.1, “Dimensions”.
cursorThe cursor that appears when the mouse is over this menubutton. See Section 4.8, “Cursors”.
directionNormally, the menu will appear below the menubutton. Set direction=LEFT to display the menu to the left of the button; use direction=RIGHT to display the menu to the right of the button; or use direction=ABOVE to place the menu above the button.
disabledforegroundThe foreground color shown on this menubutton when it is disabled.
fg or foregroundThe foreground color when the mouse is not over the menubutton.
heightThe height of the menubutton in lines of text (not pixels!). The default is to fit the menubutton's size to its contents.
highlightbackgroundColor of the focus highlight when the widget does not have focus. See Section 23, “Focus: routing keyboard input”.
highlightcolorColor shown in the focus highlight when the widget has the focus.
highlightthicknessThickness of the focus highlight.
imageTo display an image on this menubutton, set this option to the image object. See Section 4.9, “Images”.
justifyThis option controls where the text is located when the text doesn't fill the menubutton: use justify=LEFT to left-justify the text (this is the default); use justify=CENTER to center it, or justify=RIGHT to right-justify.
menuTo associate the menubutton with a set of choices, set this option to the Menu object containing those choices. That menu object must have been created by passing the associated menubutton to the constructor as its first argument. See below for an example showing how to associate a menubutton and menu.
padxHow much space to leave to the left and right of the text of the menubutton. Default is 1.
padyHow much space to leave above and below the text of the menubutton. Default is 1.
reliefNormally, menubuttons will have RAISED appearance. For other 3-d effects, see Section 4.6, “Relief styles”.
stateNormally, menubuttons respond to the mouse. Set state=DISABLED to gray out the menubutton and make it unresponsive.
textTo display text on the menubutton, set this option to the string containing the desired text. Newlines ("\n") within the string will cause line breaks.
textvariableYou can associate a control variable of class StringVar with this menubutton. Setting that control variable will change the displayed text. See Section 22, “Control variables: the values behind the widgets”.
underlineNormally, no underline appears under the text on the menubutton. To underline one of the characters, set this option to the index of that character.
widthWidth of the menubutton in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
wraplengthNormally, lines are not wrapped. You can set this option to a number of characters and all lines will be broken into pieces no longer than that number.

Here is a brief example showing the creation of a menubutton and its associated menu with three checkboxes:

    self.mb  =  Menubutton ( self, text="condiments",
                             relief=RAISED )
    self.mb.grid()
    
    self.mb.menu  =  Menu ( self.mb, tearoff=0 )
    self.mb["menu"]  =  self.mb.menu
    
    self.mayoVar  = IntVar()
    self.ketchVar = IntVar()
    self.mb.menu.add_checkbutton ( label="mayo",
                                   variable=self.mayoVar )
    self.mb.menu.add_checkbutton ( label="ketchup",
                                   variable=self.ketchVar )

This example creates a menubutton labeled condiments. When clicked, two checkbuttons labeled mayo and ketchup will drop down.