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

5. The Button widget

To create a pushbutton in a top-level window or frame named master:

    w = Button ( master, option=value, ... )

The constructor returns the new button object. Its options include:

activebackgroundBackground color when the button is under the cursor.
activeforegroundForeground color when the button is under the cursor.
anchorWhere the text is positioned on the button. See Section 4.5, “Anchors”. For example, anchor=NE would position the text at the top right corner of the button.
bd or borderwidthBorder width in pixels. Default is 2.
bg or backgroundNormal background color.
bitmapName of one of the standard bitmaps to display on the button (instead of text).
commandFunction or method to be called when the button is clicked.
cursorSelects the cursor to be shown when the mouse is over the button.
defaultNORMAL is the default; use DISABLED if the button is to be initially disabled (grayed out, unresponsive to mouse clicks).
disabledforegroundForeground color used when the button is disabled.
fg or foregroundNormal foreground (text) color.
fontText font to be used for the button's label.
heightHeight of the button in text lines (for textual buttons) or pixels (for images).
highlightbackgroundColor of the focus highlight when the widget does not have focus.
highlightcolorThe color of the focus highlight when the widget has focus.
highlightthicknessThickness of the focus highlight.
imageImage to be displayed on the button (instead of text).
justifyHow to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify.
padxAdditional padding left and right of the text. See Section 4.1, “Dimensions” for the possible values for padding.
padyAdditional padding above and below the text.
reliefSpecifies the relief type for the button (see Section 4.6, “Relief styles”). The default relief is RAISED.
stateSet this option to DISABLED to gray out the button and make it unresponsive. Has the value ACTIVE when the mouse is over it. Default is NORMAL.
takefocusNormally, keyboard focus does visit buttons (see Section 23, “Focus: routing keyboard input”), and a space character acts as the same as a mouse click, “pushing” the button. You can set the takefocus option to zero to prevent focus from visiting the button.
textText displayed on the button. Use internal newlines to display multiple text lines.
textvariableAn instance of StringVar() that is associated with the text on this button. If the variable is changed, the new value will be displayed on the button. See Section 22, “Control variables: the values behind the widgets”.
underlineDefault is -1, meaning that no character of the text on the button will be underlined. If nonnegative, the corresponding text character will be underlined. For example, underline=1 would underline the second character of the button's text.
widthWidth of the button in letters (if displaying text) or pixels (if displaying an image).
wraplengthIf this value is set to a positive number, the text lines will be wrapped to fit within this length. For possible values, see Section 4.1, “Dimensions”.

Methods on Button objects:

.flash()

Causes the button to flash several times between active and normal colors. Leaves the button in the state it was in originally. Ignored if the button is disabled.

.invoke()

Calls the button's callback, and returns what that function returns. Has no effect if the button is disabled or there is no callback.