Tkinter widgets-Button

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-
  1. import tkinter  
  2. top = tkinter.Tk()  
  3. def helloCallBack():  
  4. print("hello")  
  5. B = tkinter.Button(top, text ="Hello", command = helloCallBack)  
  6. B.pack()  
  7. 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. 
fontSet 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.
reliefRelief 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
widthUsed to set the width of the button in letters (text) or pixels (image).
wraplengthUsed to set the wrapping length.

Summary
In this chapter, you learnt what button widget is and how to perform an operation on the button click.