Button is , used to perform or execute method/function on click.
We can add a text or an image on the button.
Syntax-
B=button(main,option)
Main-parent Window.
Option-attribute of button widget.
Example-
- import tkinter
- top = tkinter.Tk()
- def helloCallBack():
- print("hello")
- B = tkinter.Button(top, text ="Hello", command = helloCallBack)
- B.pack()
- top.mainloop()
Output-

Option | Description |
activebackground | Set Background color of the button, when button is under the cursor. |
activeforeground
| Set Foreground color of button, when button is under the cursor. |
bd
| Set Border width in pixels. Default is 2.
|
bg
| Normal background color. |
command | Used to call the method or the function.
|
fg
| Set Normal foreground (text) color of the button. |
font | Set Text font of the button lable. |
height | Set Height of the button in text lines (for textual buttons) or pixels (for images). |
highlightcolor
| Change color of widgets on focus.
|
image
| Used to set an image on the button. |
justify
| Used to justify the text of a button. |
padx
| Used to add additional padding left and right of the text of the button
|
pady
| Used additional padding above and below the text of the button.
|
relief | Relief is special type of border. The values are SUNKEN, RAISED, GROOVE and RIDGE. |
underline
| Used to underline. Default is -1, which means no underline text
|
width | Used to set the width of the button in letters (text) or pixels (image). |
wraplength | Used to set the wrapping length.
|
SummaryIn this chapter, you learnt what button widget is and how to perform an operation on the button click.