Tkinter入门
前页 | Chapter 12. The Button Widget | 后页

# Options

The `Button` widget supports the following options:

Table 12-1. Button Widget Options

Option | Type | Description
--- | --- | ---
`activebackground, activeforeground` | color | The color to use when the button is activated.
`anchor` | constant | Controls where in the button the text (or image) should be located. Use one of `N`, `NE`, `E`, `SE`, `S`, `SW`, `W`, `NW`, or `CENTER`. Default is `CENTER`. If you change this, it is probably a good idea to add some padding as well, using the `padx` and/or `pady` options.
`background (bg), foreground (fg)` | color | The button color. The default is platform specific.
`bitmap` | bitmap | The bitmap to display in the widget. If the `image` option is given, this option is ignored. The following bitmaps are available on all platforms: `error`, `gray75`, `gray50`, `gray25`, `gray12`, `hourglass`, `info`, `questhead`, `question`, and `warning`.

The following additional bitmaps are available on the Macintosh only: `document`, `stationery`, `edition`, `application`, `accessory`, `folder`, `pfolder`, `trash`, `floppy`, `ramdisk`, `cdrom`, `preferences`, `querydoc`, `stop`, `note`, and `caution`.

You can also load the bitmap from an XBM file. Just prefix the filename with an at-sign, for example "`@sample.xbm`".

`borderwidth (bd)` | int | The width of the button border. The default is platform specific, but is usually 1 or 2 pixels.
`command` | callback | A function or method that is called when the button is pressed. The callback can be a function, bound method, or any other callable Python object.
`cursor` | cursor | The cursor to show when the mouse is moved over the button.
`default` | constant | If set, the button is a default button. Tk will indicate this by drawing a platform specific indicator (usually an extra border). NOTE: The syntax has changed in 8.0b2!!!
`disabledforeground` | color | The color to use when the button is disabled. The background is shown in the `background` color.
`font` | font | The font to use in the button. The button can only contain text in a single font.
`highlightbackground, highlightcolor` | color | Controls how to draw the focus highlight border. When the widget has focus, the border is drawn in the `highlightcolor` color. Otherwise, it is drawn in the `highlightbackground` color. The defaults are system specific.
`highlightthickness` | distance | Controls the width of the focus highlight border. Default is typically one or two pixels.
`image` | image | The image to display in the widget. If specified, this takes precedence over the `text` and `bitmap` options.
`justify` | constant | Defines how to align multiple lines of text. Use `LEFT`, `RIGHT`, or `CENTER`.
`padx, pady` | distance | Button padding. These options specify the horizontal and vertical padding between the text or image, and the button border.
`relief` | constant | Border decoration. Usually, the button is `SUNKEN` when pressed, and `RAISED` otherwise. Other possible values are `GROOVE`, `RIDGE`, and `FLAT`.
`state` | constant | The button state: `NORMAL`, `ACTIVE` or `DISABLED`. Default is `NORMAL`.
`takefocus` | flag | Indicates that the user can use the Tab key to move to this button. Default is an empty string, which means that the button accepts focus only if it has any keyboard bindings (default is on, in other words).
`text` | string | The text to display in the button. The text can contain newlines. If the `bitmap` or `image` options are used, this option is ignored.
`textvariable` | variable | Associates a Tkinter variable (usually a `StringVar`) to the button. If the variable is changed, the button text is updated.
`underline` | int | Which character to underline, in a text label. Default is -1, which means that no character is underlined.
`width, height` | distance | The size of the button. If the button displays text, the size is given in text units. If the button displays an image, the size is given in pixels (or screen units). If the size is omitted, it is calculated based on the button contents.
`wraplength` | distance | Determines when a button's text should be wrapped into multiple lines. This is given in screen units. Default is no wrapping.

前页 | Home | 后页
Helpers | Up | The Canvas Widget