Python Tkinter Button Widget
The Tkinter button widget is used to add buttons to Python applications. Text or images can be placed on the button. The button can be used to listen to user actions and can be associated with a Python function. When the button is pressed, the function is called automatically.
Syntax
The syntax format is as follows:
w = Button ( master, option=value, ... )
master: The parent container of the button.
options: Optional items, i.e., the configurable properties of the button. These options can be set in the form of key = value, separated by commas.
| Serial No. | Options & Description |
|---|---|
| 1 | activebackground The background color of the button when the mouse hovers over it |
| 2 | activeforeground The foreground color of the button when the mouse hovers over it |
| 3 | bd The size of the button border, default is 2 pixels |
| 4 | bg The background color of the button |
| 5 | command The function associated with the button; when the button is clicked, this function is executed |
| 6 | fg The foreground color of the button (the color of the button text) |
| 7 | font Text font |
| 8 | height The height of the button |
| 9 | highlightcolor The color to highlight |
| 10 | image The image to display on the button |
| 11 | justify When displaying multiple lines of text, sets the alignment between different lines. Options include LEFT, RIGHT, CENTER |
| 12 | padx The padding of the button in the x-axis direction, which is the distance between the button's content and the button's edge |
| 13 | pady The padding of the button in the y-axis direction |
| 14 | relief Border style, sets the 3D effect of the widget. Options: FLAT, SUNKEN, RAISED, GROOVE, RIDGE. Default is FLAT. |
| 15 | state Sets the state of the button widget. Options: NORMAL, ACTIVE, DISABLED. Default is NORMAL. |
| 16 | underline Underline. By default, the text on the button has no underline. The value is the string index for the underline. When it is 0, the first character is underlined; when it is 1, the first two characters are underlined, and so on. |
| 17 | width The width of the button. If this option is not set, its size adapts to the button's content (the size of the text or image). |
| 18 | wraplength Limits the number of characters displayed per line on the button |
| 19 | text The text content of the button |
| 19 | anchor Anchor option, controls the position of the text, default is center |
Methods
The following are the commonly used methods of the widget:| Method | Description |
|---|---|
| deselect() | Clears the state of the radio button |
| flash() | Flashes the radio button a few times between the active state color and the normal color, but keeps it in its starting state. |
| invoke() | You can call this method to obtain the same action that occurs when the user clicks the radio button to change its state. |
| select() | Sets the radio button to selected. |
Examples
In the example, clicking the button will display a message:
Examples
The test output is as follows:
Other extensions
Python GUI Programming