How to use the Tkinter button widget

Tkinter is a Python in-built GUI module. It is used to develop cross-platform desktop applications. It is lightweight and easy to use compared to other GUI modules available in python. In this Answer, we will learn to use the button widget in Tkinter.

The button widget performs an action when it is clicked.

Syntax

Button()

Parameters

Let's go through some of the button parameters.

  • activebackground: Displays the background color when we hover over the button.

  • activeforeground: Displays the foreground color when we hover over the button.

  • bd: Provides a border to the button. We can provide a border in pixels and the default is 2 pixels.

  • bg: Provides the background color of the button.

  • command: This parameter accepts a function or method to execute when the button is clicked.

  • fg: Provides the foreground color of the button.

  • font: Provides the font for the button label.

  • image: Displays an image instead of text on the button.

  • padx: Provides horizontal padding for the text.

  • pady: Provides vertical padding for the text.

  • state: Provides the state of the button, which can be DISABLED, ACTIVE, or NORMAL.

Let's look at an example of Button().

Example

#import tkinter module
import tkinter as tk

#create window
window = tk.Tk()

#provide size to window
window.geometry("300x300")

def sayHello():
	#add text label to window
	tk.Label(window, text="Hello From Educative !!!").pack()

#create button
tk.Button(window, text ="CLICK HERE", command = sayHello).pack()

window.mainloop()

Explanation

  • Line 5: We create an instance of the Tkinter class Tk() and assign it to the variable window.

  • Line 8: We provide the window size as 300x300.

  • Line 10: We define a function sayHello to execute when the button is clicked.

  • Line 12: We display the label when the button is clicked using the widget Label.

  • Line 15: We create a button that shows the text CLICK HERE and executes the function sayHello when it is clicked.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved