Python Tkinter Button Widget

The Button widget in Tkinter is mainly used to add a button in any GUI Application. In Python, while using the Tkinter button widget, we can easily modify the style of the button like adding a background colors to it, adjusting height and width of button, or the placement of the button, etc. very easily.

  • You can add various types of buttons(as per your applications UI) to your application with the help of the Button Widget.
  • You can also associate any method or function with a button if you want and then that method will get called whenever you press the button.
  • There are many options of button widget which you can reset or set according to your requirements.

Tkinter Button Widget

The syntax of the button widget is given below,

W = Button(master, options)   

In the above syntax, the master parameter denotes the parent window. You can use many options to change the look of the buttons and these options are written as comma-separated.

Tkinter Button Widget Options:

Following are the various options used with tkinter button widgets:

Option nameDescription
activebackgroundThis option indicates the background of the button at the time when the mouse hovers the button.
bdThis option is used to represent the width of the border in pixels.
bgThis option is used to represent the background color of the button.
commandThe command option is used to set the function call which is scheduled at the time when the function is called.
activeforegroundThis option mainly represents the font color of the button when the mouse hovers the button.
fgThis option represents the foreground color of the button.
fontThis option indicates the font of the button.
heightThis option indicates the height of the button. This height indicates the number of text lines in the case of text lines and it indicates the number of pixels in the case of images.
imageThis option indicates the image displayed on the button.
higlightcolorThis option indicates the highlight color when there is a focus on the button
justifyThis option is used to indicate the way by which the multiple text lines are represented. For left justification, it is set to LEFT and it is set to RIGHT for the right justification, and CENTER for the center justification.
padxThis option indicates the additional padding of the button in the horizontal direction.
padyThis option indicates the additional padding of the button in the vertical direction.
underlineThis option is used to underline the text of the button.
widthThis option specifies the width of the button. For textual buttons, It exists as a number of letters or for image buttons it indicates the pixels.
WraplengthIn the case, if this option’s value is set to a positive number, the text lines will be wrapped in order to fit within this length.
stateThis option’s value set to DISABLED to make the button unresponsive. The ACTIVE mainly represents the active state of the button.

We will be using the different options in examples below.

Tkinter Button Widget Example

Now let us create a simple submit button with the help of code snippet given below:

from tkinter import *   
win = Tk()  ## win is a top or parent window
b = Button(win, text = "Submit")  
b.pack()  #using pack() geometry

In the above code example, we created a simple window of given width and height. Then we added a button widget to it with providing the window created as the master of that button and adding a text for the button.

Tkinter Button Widget – Add style and Event handler

Below we have another code snippet where we will change the look of buttons by adding morestyle to it. Let us see how we do it:

import tkinter
from tkinter import *
from tkinter import messagebox

top = Tk()
def click():
    messagebox.showinfo("Hello", "Green Button clicked")
a = Button(top, text="yellow", activeforeground="yellow", activebackground="orange", pady=10)
b = Button(top, text="Blue", activeforeground="blue", activebackground="orange", pady=10)
# adding click function to the below button
c = Button(top, text="Green", command=click, activeforeground = "green", activebackground="orange", pady=10)
d = Button(top, text="red", activeforeground="yellow", activebackground="orange", pady=10)

a.pack(side = LEFT)
b.pack(side = RIGHT)
c.pack(side = TOP)
d.pack(side = BOTTOM)

In the above code, we have added some styling using different options and we have added an event handler to handle the click event of the 3rd button. So whenever you will make a click on the button with text Green, you will see a Tkinter messagebox with a message.

Follow Us On