Python Tkinter Button Introduction

aimtocode

Python Tkinter Button

Buttons are standard widgets in a GUI. They come with the default Tkinter module and you can place them in your window.

Python allows us to configure the look of the button according to our requirements.

You can also associate a method or function with a button which is called when the button is pressed.

Syntax:

  							   
 W = Button(parent, options)


Example 1: Simple Button



Output:


Example 2: Button onclick event

This will print Welcome to Aimtocode"after clicking on "Click me" button.

Output: Before Clicking


Output: After Clicking

Example 3: Button Event



Output: Before clicking the button

Output: After clicking the button

You can set the following important properties to customize a button:

Option Description
activebackground It represents the background of the button when the mouse hover the button.
activeforeground It represents the font color of the button when the mouse hover the button.
Bd It represents the border width in pixels.
Bg It represents the background color of the button.
Command It is set to the function call which is scheduled when the function is called.
Fg Foreground color of the button.
Font The font of the button text.
Height The height of the button. The height is represented in the number of text lines for the textual lines or the number of pixels for the images.
Highlightcolor The color of the highlight when the button has the focus.
Image It is set to the image displayed on the button.
justify It illustrates the way by which the multiple text lines are represented. It is set to LEFT for left justification, RIGHT for the right justification, and CENTER for the center.
Padx Additional padding to the button in the horizontal direction.
pady Additional padding to the button in the vertical direction.
Relief It represents the type of the border. It can be SUNKEN, RAISED, GROOVE, and RIDGE.
State This option is set to DISABLED to make the button unresponsive. The ACTIVE represents the active state of the button.
Underline Set this option to make the button text underlined.
Width The width of the button. It exists as a number of letters for textual buttons or pixels for image buttons.
Wraplength If the value is set to a positive number, the text lines will be wrapped to fit within this length.
aimtocode