Python Tkinter Button

By | April 6, 2020

In this tutorial, we will learn about Python Tkinter Button. This Python Tkinter widget is used to add different kinds of buttons in our Python application. You can set configurations of the button according to the requirements of your application. Furthermore, we can set functionality to our button that what will happen when this button will get pressed. In this tutorial, we will see different Tkinter button options that how we can customize our Button.

Syntax to add Python Tkinter Button:

w = Button (master, options)
  • master: This represents the parent window.
  • options: Here is the list of most commonly used options for this widget.

Example program of Tkinter button:

Without wasting time I would like to start with this example program that how we can add Python Tkinter Button in our application.

from tkinter import *  

#setting up main window 
mainWindow = Tk()  
  
#setting size of main window 
mainWindow.geometry("200x100")  

#Creating new button  
myButton = Button(mainWindow,text = "Submit")  
  
myButton.pack()  

mainWindow.mainloop()

Output:

Python Tkinter Button

Example program of Button click action in Python:

In this example program, we will learn how we can perform some action when our button will get clicked.

from tkinter import *  
from tkinter import messagebox

#setting up main window 
mainWindow = Tk()  
  
#setting size of main window 
mainWindow.geometry("200x100")  

# defining a function
def show():  
    messagebox.showinfo("Info", "Hello, You Clicked this Button")  

#Creating new button  
myButton = Button(mainWindow,command = show, text = "Submit")  
  
myButton.pack()  

mainWindow.mainloop() 

Various possible options in Python Tkinter button:

OptionDescription
activebackgroundThis option helps us to set background color of button on mouse hover.
activeforegroundThis option helps us to set font color of button on mouse hover.
BdThis option helps us to set border width in pixel.
BgThis option helps us to set the background colour button.
FgThis option helps us to set foreground color button.
FontThis option helps us to set font of button.
HeightThis option helps us to set height of button.
HighlightcolorThis option helps us to set Highlight color when button will get focus.
ImageThis option helps us to set image displayed on button.
PadxThis option helps us to set horizontal padding in button.
PadyThis option helps us to set vertical padding in button.
ReliefThis option helps us to set type of border on button like SUNKEN, RAISED, GROOVE, and RIDGE..
StateThis option helps us to set button disable.
UnderlineThis option helps us to set button text underline.
WidthThis option helps us to set width of button

Python Tkinter Button Functions:

There are various Python Button Button functions available. Which makes it so easy to use button widget in our application these functions are as follow.

FunctionDescription
flash()This function helps us to flash widget many times between active and normal colours.
invoke()This function helps us to invoke the associated method with Python Tkinter Button.