The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button.
SyntaxHere is the simple syntax to create this widget −
w = Button ( master, option=value, ... )Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
Activebackground
activebackground Background color when the button is under the cursor.
2Activeforeground
Foreground color when the button is under the cursor.
3Bd
Border width in pixels. Default is 2.
4Bg
Normal background color.
5Command
Function or method to be called when the button is clicked.
6Fg
Normal foreground (text) color.
7Font
Text font to be used for the button's label.
8Height
Height of the button in text lines (for textual buttons) or pixels (for images).
9Highlightcolor
The color of the focus highlight when the widget has focus.
10Image
Image to be displayed on the button (instead of text).
11Justify
How to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify.
12Padx
Additional padding left and right of the text.
13Pady
Additional padding above and below the text.
14Relief
Relief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE.
15State
Set 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.
16Underline
Default is -1, meaning that no character of the text on the button will be underlined. If nonnegative, the corresponding text character will be underlined.
17Width
Width of the button in letters (if displaying text) or pixels (if displaying an image).
18Wraplength
If this value is set to a positive number, the text lines will be wrapped to fit within this length.
MethodsFollowing are commonly used methods for this widget −
Sr.No. Method & Description 1flash()
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.
2invoke()
Calls the button's callback, and returns what that function returns. Has no effect if the button is disabled or there is no callback.
ExampleTry the following example yourself −
from tkinter import * from tkinter import messagebox top = Tk() top.geometry("100x100") def helloCallBack(): msg=messagebox.showinfo( "Hello Python", "Hello World") B = Button(top, text ="Hello", command = helloCallBack) B.place(x=50,y=50) top.mainloop()
When the above code is executed, it produces the following result −
python_gui_programming.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4