The button widget is a standard tkinter part that implements a variety of buttons. Buttons can contain text or images, and you can call Python functions or methods for each button. The function or method is called automatically when the Tkinter button is pressed.
The button can only display text in a single font, but the text may span more than one line. Additionally, a character can have an underscore, such as a labeled keyboard shortcut. By default, the TAB key can be used to move to a button part.
You typically use toolbar buttons in the application window, and accept or dismiss the data entered in the dialog box.
Button Property
Function |
Describe |
Text |
Display text content |
Command |
Event handler for the specified button |
Compound |
Specify the position relationship of the text to the image |
Bitmap |
Refers to the location map |
Focus_set |
Sets the focus of the current component |
Master |
Represents the parent window |
Bg |
Set Background color |
Fg |
Set foreground color |
Font |
Set Font size |
Height |
Sets the display height, if this item is not set, its size to fit the content label |
Relief |
Specifies the label near the border of the cosmetic decoration, the default is flat, can be set parameters; Flat, Groove, raised, ridge, solid, sunken |
Width |
Sets the display width, if this item is not set, its size to fit the content label |
Wraplength |
Set this option to the number of characters that you want to limit per line, and the number defaults to 0 |
State |
Set component state; normal, active (active), disabled (disabled) |
Anchor |
Set where button text appears on the control Available values: N (North), S (south), W (west), E (East), and NE,NW,SE,SW |
Bd |
Sets the border size of the button; BD (bordwidth) defaults to 1 or 2 pixels |
Textvariable |
Setting the button with the Textvariable property |
Button Buttons method
Here are the widgets that are commonly used by buttons
Method |
Describe |
Flash () |
Flash the button. This method redraws the button several times, alternating between active and normal appearance. |
Invoke () |
Invoke the command associated with the button. |
Python Tkinter Button Sample code
4 button buttons are created, different properties are set
Width,height,relief,bg,bd,fg,state,bitmap,command,anchor
fromTkinterImport* fromTkinterImportMessageboxroot=Tk () root.title ("Button Test")defcallback (): Messagebox.showinfo ("Python Command","life is short, I use Python")"""Create 4 button buttons, and set Width,height,relief,bg,bd,fg,state,bitmap,command,anchor"""Button (root, Text="labels near the border of the exterior decoration", width=21,height=3,relief=raised). Pack () Button (root, Text="Set Button State", width=21,state=DISABLED). Pack () Button (root, Text="set bitmap to the left position of the button", compound=" Left", bitmap="Error"). Pack () Button (root, Text="set command event invoke commands", fg="Blue", bd=7,width=28,command=callback). Pack () Button (root, Text="Set height width and text display position", anchor ='SW', width = 30,height = 2). Pack () Root.mainloop ()
View Code
Python tkinter button