The Wayback Machine - https://web.archive.org/web/20241126231216/https://www.geeksforgeeks.org/python-creating-a-button-in-tkinter/
Open In App

Python Tkinter – Create Button Widget

Last Updated : 14 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Tkinter Button widget is a graphical control element used in Python’s Tkinter library to create clickable buttons in a graphical user interface (GUI). It provides a way for users to trigger actions or events when clicked.

Note: For more reference, you can read our article:

  1. What is Widgets
  2. Python Tkinter Overview
  3. Python Tkinter Tutorial

Tkinter Button Widget Syntax

The syntax to use the button widget is given below.

Syntax: Button(parent, options)

Parameters

  • parent: This parameter is used to represents the parent window.
  • options:There are many options which are available and they can be used as key-value pairs separated by commas.

Tkinter Button Options

  • activebackground: Background color when the button is under the cursor.
  • activeforeground: Foreground color when the button is under the cursor.
  • anchor: Width of the border around the outside of the button
  • bd or borderwidth: Width of the border around the outside of the button
  • bg or background: Normal background color.
  • command: Function or method to be called when the button is clicked.
  • cursor: Selects the cursor to be shown when the mouse is over the button.
  • text: Text displayed on the button.
  • disabledforeground: Foreground color is used when the button is disabled.
  • fg or foreground: Normal foreground (text) color.
  • font: Text font to be used for the button’s label.
  • height: Height of the button in text lines
  • highlightbackground: Color of the focus highlight when the widget does not have focus.
  • highlightcolor: The color of the focus highlight when the widget has focus.
  • highlightthickness: Thickness of the focus highlight.
  • image: Image to be displayed on the button (instead of text).
  • justify: tk.LEFT to left-justify each line; tk.CENTER to center them; or tk.RIGHT to right-justify.
  • overrelief: The relief style to be used while the mouse is on the button; default relief is tk.RAISED.
  • padx, pady: padding left and right of the text. / padding above and below the text.
  • width: Width of the button in letters (if displaying text) or pixels (if displaying an image).
  • underline: Default is -1, underline=1 would underline the second character of the button’s text.
  • width: Width of the button in letters
  • wraplength: If this value is set to a positive number, the text lines will be wrapped to fit within this length.

Methods

  1. flash(): Causes the button to flash several times between active and normal colors. Leaves the button in the state it was in originally. Ignored if the button is disabled.
  2. invoke(): Calls the button’s command callback, and returns what that function returns. Has no effect if the button is disabled or there is no callback.

Creating a Button using Tkinter

In this example, below code uses the tkinter library to create a graphical user interface. It defines a function, button_clicked(), which prints a message when called. Then, it creates a tkinter window (root) and a button within it, configured with various options like text, color, font, and behavior.

Python
import tkinter as tk

def button_clicked():
    print("Button clicked!")

root = tk.Tk()

# Creating a button with specified options
button = tk.Button(root, 
                   text="Click Me", 
                   command=button_clicked,
                   activebackground="blue", 
                   activeforeground="white",
                   anchor="center",
                   bd=3,
                   bg="lightgray",
                   cursor="hand2",
                   disabledforeground="gray",
                   fg="black",
                   font=("Arial", 12),
                   height=2,
                   highlightbackground="black",
                   highlightcolor="green",
                   highlightthickness=2,
                   justify="center",
                   overrelief="raised",
                   padx=10,
                   pady=5,
                   width=15,
                   wraplength=100)

button.pack(padx=20, pady=20)

root.mainloop()

Output

2024-04-2417-34-05online-video-cuttercom-ezgifcom-video-to-gif-converter

Button Widget

Creation of Button without using TK Themed Widget

Creation of Button using tk themed widget (tkinter.ttk). This will give you the effects of modern graphics. Effects will change from one OS to another because it is basically for the appearance.

As you can observe that BORDER is not present in 2nd output because tkinter.ttk does not support border. Also, when you hover the mouse over both the buttons ttk.Button will change its color and become light blue (effects may change from one OS to another) because it supports modern graphics while in the case of a simple Button it won’t change color as it does not support modern graphics.

Python
# import tkinter module 
from tkinter import *        

# Following will import tkinter.ttk module and 
# automatically override all the widgets 
# which are present in tkinter module. 
from tkinter.ttk import *

# Create Object
root = Tk() 

# Initialize tkinter window with dimensions 100x100             
root.geometry('100x100')     

btn = Button(root, text = 'Click me !', 
                command = root.destroy) 

# Set the position of button on the top of window 
btn.pack(side = 'top')     

root.mainloop() 

Output


Note: The tkinter.ttk module provides access to the Tk-themed widget set, introduced in Tk 8.5. If Python has not been compiled against Tk 8.5, this module can still be accessed if Tile has been installed. The former method using Tk 8.5 provides additional benefits including anti-aliased font rendering under X11 and window transparency.
The basic idea for tkinter.ttk is to separate, to the extent possible, the code implementing a widget’s behavior from the code implementing its appearance. tkinter.ttk is used to create modern GUI (Graphical User Interface) applications that cannot be achieved by tkinter itself. 

Python Tkinter – Create Button Widget – FAQ

How to Create a Button Widget in Python

Creating button widgets in Python is commonly done using various GUI frameworks. One of the most popular frameworks for creating graphical user interfaces in Python is Tkinter. Here’s how you can create a button widget using Tkinter

How to Create a Widget in Tkinter

A widget in Tkinter can be any element of the GUI such as a button, label, text box, etc. To create a widget:

  1. Import Tkinter: First, you need to import the Tkinter module.
  2. Create a Main Window: Create the main application window.
  3. Add Widgets: Add widgets like buttons, labels, text boxes to the main window.

Example of creating a simple button:

import tkinter as tk

def on_button_click():
print("Button clicked!")

root = tk.Tk()
root.title("Tkinter Button Example")

# Creating a button widget
button = tk.Button(root, text="Click Me", command=on_button_click)
button.pack()

root.mainloop()

How to Assign a Function to a Button in Tkinter

To assign a function to a button in Tkinter, you use the command parameter of the Button class. The command parameter takes a function name that will be called when the button is clicked. Here’s how to do it:

import tkinter as tk

def greet():
print("Hello, welcome to the app!")

root = tk.Tk()
button = tk.Button(root, text="Greet", command=greet)
button.pack()
root.mainloop()

How to Make a Clickable Button in Tkinter

Making a clickable button in Tkinter involves assigning a command to the button, which is executed when the button is clicked. The above examples demonstrate this by using the command attribute of the Button widget. Here’s another simple example:

import tkinter as tk

def say_hello():
print("Hello!")

app = tk.Tk()
app.title("Clickable Button Example")

# Button with command
hello_button = tk.Button(app, text="Say Hello", command=say_hello)
hello_button.pack()

app.mainloop()

What is a Widget GUI in Python?

In the context of Python GUI development, a widget is a graphical control element, such as a button, label, text entry field, or slider, that interacts with the user. Widgets are the building blocks of a graphical user interface (GUI) and are used to create interactive applications. Python offers several libraries for GUI development, with Tkinter being the most commonly included with Python for creating widgets



Previous Article
Next Article

Similar Reads

How to Set Text of Tkinter Text Widget With a Button?
Prerequisite: Python GUI – tkinter Text Widget is used where a user wants to insert multi-line text fields. In this article, we are going to learn the approaches to set the text inside the text fields of the text widget with the help of a button. Approach: Using insert and delete methodImport the Tkinter module.Create a GUI window.Create our text w
2 min read
How to Clear the Entry Widget After Button Press in Tkinter
The entry widget in Tkinter is a common input field that allows users to enter and edit a single line of text. Clearing the contents of this widget programmatically can enhance user interaction, especially after submitting or resetting a form. In this article, we will explore two different approaches to clear the entry after a button is pressed in
2 min read
Create Find and Replace features in Tkinter Text Widget
Prerequisites: Python GUI – tkinterFirstly we need to find all the words or letters that we want to replace in our below text for that we will use the find function in which we will determine all the starting and the ending indexes from the editor of the same word after that we will apply the replace functionality in which we will delete that part
4 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
How to create a pop up message when a button is pressed in Python - Tkinter?
In this article, we will see how to create a button and how to show a popup message when a button is pressed in Python. Tkinter is a standard Python Package for creating GUI applications. Tkinter may be a set of wrappers that implement the Tk widgets as Python classes. Python, when combined with Tkinter, provides a quick and straightforward way to
2 min read
Python | Menu widget in Tkinter
Tkinter is Python’s standard GUI (Graphical User Interface) package. It is one of the most commonly used package for GUI applications which comes with the Python itself. Menus are the important part of any GUI. A common use of menus is to provide convenient access to various operations such as saving or opening a file, quitting a program, or manipu
2 min read
Progressbar widget in Tkinter | Python
The purpose of this widget is to reassure the user that something is happening. It can operate in one of two modes - In determinate mode, the widget shows an indicator that moves from beginning to end under program control. In indeterminate mode, the widget is animated so the user will believe that something is in progress. In this mode, the indica
2 min read
Python | PanedWindow Widget in Tkinter
Tkinter supports a variety of widgets to make GUI more and more attractive and functional. The PanedWindow widget is a geometry manager widget, which can contain one or more child widgets panes. The child widgets can be resized by the user, by moving separator lines sashes using the mouse. Syntax: PanedWindow(master, **options) Parameters: master:
3 min read
Python Tkinter - Toplevel Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in Python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter Toplevel widget A Toplevel widget is used to create a window on top of all other w
3 min read
Combobox Widget in tkinter | Python
Python provides a variety of GUI (Graphic User Interface) types such as PyQT, Tkinter, Kivy, WxPython, and PySide. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to understand. The word Tkinter comes from the Tk interface. The tkinter module is available in Python standard library which has to be impo
3 min read
Python Tkinter - ListBox Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter ListBox widget The ListBox widget is used to display different types of items. The
2 min read
Python Tkinter - Scale Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter Scale widget The Scale widget is used whenever we want to select a specific value
2 min read
Python Tkinter - Canvas Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs.Note: For more information, refer to Python GUI – tkinter Canvas widget The Canvas widget lets us display various graphics on the application
3 min read
Python Tkinter - MessageBox Widget
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy ta
2 min read
Python Tkinter - Menubutton Widget
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy ta
4 min read
Creating Tabbed Widget With Python-Tkinter
Python offers a range of GUI frameworks that can be used to develop GUI based applications in Python. The most widely used Python interface is Tk interface or tkinter( as renamed in Python 3.x) . The Tkinter module offers a wide range of widgets that can be used to develop GUI applications much faster and easier compared to the other interfaces off
5 min read
Python Tkinter - Validating Entry Widget
Python offers a variety of frameworks to work with GUI applications. Tkinter or Tk interface is one of the most widely used Python interface to build GUI based applications. There are applications that require validation of text fields to prevent invalid input from the user before the form is submitted. Python allows input validation by allowing va
5 min read
Python Tkinter - ScrolledText Widget
Tkinter is a built-in standard python library. With the help of Tkinter, many GUI applications can be created easily. There are various types of widgets available in Tkinter such as button, frame, label, menu, scrolledtext, canvas and many more. A widget is an element that provides various controls. ScrolledText widget is a text widget with a scrol
3 min read
Python Tkinter – How to display a table editor in a text widget?
In this article, we will discuss how to display a table editor in a text widget. Before moving ahead let's create a simple text widget first with the help of Tkinter. For the link to the Excel file used in the code click here. Text Widget The Text widget is used to show the text editor in the Tkinter window. A user can modify the text in the text e
3 min read
Python Tkinter - Frame Widget
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.
3 min read
Python Tkinter - Checkbutton Widget
The Checkbutton widget is a standard Tkinter widget that is used to implement on/off selections. Checkbuttons can contain text or images. When the button is pressed, Tkinter calls that function or method. Note: For more reference, you can read our article, What is WidgetsPython Tkinter OverviewPython Tkinter TutorialTkinter Checkbutton Widget Synta
5 min read
Hide Python Tkinter Text widget border
Tkinter is a standard GUI library for Python, providing tools to create graphical user interfaces easily. When designing interfaces, developers often encounter the need to customize widget appearances, including removing widget borders. Borders can sometimes clash with the desired aesthetic or layout of the application. This article explores what w
3 min read
Validating Entry Widget In Python Tkinter
When developing graphical user interfaces (GUIs) in Python, Tkinter is a powerful and widely-used toolkit that simplifies the process. One of the common tasks in GUI applications is obtaining user input through form fields. Tkinter's Entry widget is designed for this purpose, allowing users to input text. However, ensuring the correctness of the en
7 min read
Access the Actual Tab Widget of ttk.Notebook in Python Tkinter
Tab widgets of ttk.Notebook are essential components in a Tkinter GUI for organizing content into multiple tabs. Accessing these tab widgets allows for customization and manipulation of their properties. In this article, we will explore two different approaches to accessing the actual tab widget of ttk.Notebook. Access the actual tab widget of ttk.
2 min read
Python Tkinter - Text Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs.Note: For more information, refer to Python GUI – tkinter  Text WidgetText Widget is used where a user wants to insert multiline text fields.
5 min read
Python Tkinter - Entry Widget
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.In Python3 Tkinter is come preinstalled But you can also install it by using
5 min read
How do you create a Button on a tkinter Canvas?
In this article, we will see how to create a button on a Tkinter Canvas. The Canvas widget display various graphics on the application. It can be used to draw simple shapes to complicated graphs. We can also display various kinds of custom widgets according to our needs. It is used trigger any function which is presented in the code Intro to code :
1 min read
Create an Import File Button with Tkinter
Graphical User Interfaces (GUIs) play a crucial role in enhancing the user experience of applications. Tkinter, a built-in Python library, provides a simple way to create GUI applications. In this article, we will focus on creating an "Import File" button using Tkinter. Create An Import File Button With TkinterIn this section, we will create an imp
3 min read
Tkinter | Adding style to the input text using ttk.Entry widget
Tkinter is a GUI (Graphical User Interface) module which is widely used to create GUI applications. It comes along with the Python itself. Entry widgets are used to get the entry from the user. It can be created as follows- entry = ttk.Entry(master, option = value, ...) Code #1: Creating Entry widget and taking input from user (taking only String d
2 min read
How to change background color of Tkinter OptionMenu widget?
Prerequisites: Tkinter While creating GUI applications, there occur various instances in which you need to make selections among various options available. For making such choices, the Option Menu widget was introduced. In this article, we will be discussing the procedure of changing menu background color of Tkinter’s option Menu widget. To achieve
2 min read
three90RightbarBannerImg