In this tutorial, we are going to learn about the button widget in tkinter, we will be looking at how to display button on the screen as well as discuss the features and their properties in detail.

Recommended Book: Python GUI Programming with Tkinter

What is a Button in Tkinter?

The button widget in Tkinter is used to add different types of buttons on the main window that you are working on. You can modify button settings to your own requirements. You can also add a function to the button and call whenever the button is pressed. You can create a button as follows:

button_tk = Button(window, features) 

Discover how to create visually stunning and feature-rich applications by using Python’s built-in Tkinter GUI toolkit

Let’s look at the properties of button features:

1 activebackground Shows the button background color upon the mouse hover
2 activeforeground Shows the button font color upon the mouse hover
3 Bd It shows the width of the border in pixels
4 Bg It shows the background color of the button 
5 Command It is used to set the function call whenever it is called.
6 Fg Shows the foreground color of the button 
7 Font It decides the font of the button text
8 Height It decides the height of the button. 
10 Highlightcolor It shows the highlight color of the button when it is clicked
11 Image It is used to set image on the button 
12 justify It is used to display text on button in RIGHT, LEFT and CENTER justified positions.
13 Padx This adds padding in horizontal direction
14 pady This adds padding in vertical direction
15 Relief This displays different types of borders like SUNKEN, RAISED, GROOVE, and RIDGE.
17 State This represents DISABLED or ACTIVE state of the button
18 Underline It is used to underline the text present on the button
19 Width This feature sets the width of the button
20 Wraplength This is used to wrap text lines within the given length.

Creating a Simple Button

import tkinter as tk
screen = tk.Tk()
#Screen size
button_tk = tk.Button(screen, text="Hello") 


Button Tkinter

Creating a Button With a Function

We have created a simple function to alert the user that the button has been clicked. To learn more about functions in Python, check here.

import tkinter as tk
screen = tk.Tk()

#Screen size

#Define Function for Button
def alert():
   print("Button is clicked!")

button_tk = tk.Button(screen, text="Hello", command=alert) 



Once the button is clicked, output on the python IDE will be:

Button is clicked!