Signup/Sign In

Python Tkinter Button Widget

In this tutorial, we will cover the Button widget of Tkinter module in Python.

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 name Description
activebackground This option indicates the background of the button at the time when the mouse hovers the button.
bd This option is used to represent the width of the border in pixels.
bg This option is used to represent the background color of the button.
command The command option is used to set the function call which is scheduled at the time when the function is called.
activeforeground This option mainly represents the font color of the button when the mouse hovers the button.
fg This option represents the foreground color of the button.
font This option indicates the font of the button.
height This 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.
image This option indicates the image displayed on the button.
higlightcolor This option indicates the highlight color when there is a focus on the button
justify This 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.
padx This option indicates the additional padding of the button in the horizontal direction.
pady This option indicates the additional padding of the button in the vertical direction.
underline This option is used to underline the text of the button.
width This 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.
Wraplength In 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.
state This 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
  
win.geometry("200x100")  
  
b = Button(win, text = "Submit")  
  
b.pack()  #using pack() geometry
  
win.mainloop()  

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()
top.geometry("300x150")
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)
top.mainloop()

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.

Summary:

In this tutorial, we learned how to make a Tkinter Button widget with various options like chaning the style of the button, adding text to the button or positioning the button. We also saw a code example for adding an event handler function to any button to perform some action when the button is clicked.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight