A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-creating-a-button-in-tkinter/ below:

Python Tkinter - Create Button Widget

Python Tkinter - Create Button Widget

Last Updated : 11 Jul, 2025

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

Tkinter Button Options 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

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. 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4