Python Tkinter Button

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()

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 Tkinter button:

Option Description
activebackground This option helps us to set background color of button on mouse hover.
activeforeground This option helps us to set font color of button on mouse hover.
Bd This option helps us to set border width in pixel.
Bg This option helps us to set the background colour button.
Fg This option helps us to set foreground color button.
Font This option helps us to set font of button.
Height This option helps us to set height of button.
Highlightcolor This option helps us to set Highlight color when button will get focus.
Image This option helps us to set image displayed on button.
Padx This option helps us to set horizontal padding in button.
Pady This option helps us to set vertical padding in button.
Relief This option helps us to set type of border on button like SUNKEN, RAISED, GROOVE, and RIDGE..
State This option helps us to set button disable.
Underline This option helps us to set button text underline.
Width This option helps us to set width of button

Tkinter Button Methods:

After learning about various available options in Python Tkinter Button. Its time to check out some available methods for Tkinter Button widget.

METHOD DESCRIPTION
flash() This method helps us to flash widget many times between active and normal colours.
invoke() This method helps us to invoke the associated method with Python Tkinter Button.
Spread the love
Scroll to Top
×