Python Tkinter Button

The Button widget in Tkinter is used to create a clickable button that can execute a command when clicked.

Syntax

</>
Copy
tkinter.Button(master, text, command, **options)

Parameters

ParameterTypeDescription
masterWidgetThe parent widget, usually a frame or root window.
textStringThe text displayed on the button.
commandFunctionThe function to execute when the button is clicked.
**optionsVariousAdditional properties like background color, font, size, width, height, etc.

Return Value

Returns a Button widget that can be displayed in a Tkinter window.


Examples

1. Creating a Simple Button

This example creates a basic button that prints a message when clicked.

</>
Copy
import tkinter as tk

# Create the main application window
root = tk.Tk()
root.title("Tkinter Button Example - tutorialkart.com")
root.geometry("400x200")

# Define the button click function
def on_click():
    print("Button clicked!")

# Create a button
button = tk.Button(root, text="Click Me", command=on_click)

# Display the button
button.pack()

# Run the application
root.mainloop()

Screenshot in macOS

Screenshot in Windows

A button labeled Click Me appears. Clicking the button prints Button clicked! in the console.

2. Button with Styling

Adding styling options such as background color, text color, and font.

</>
Copy
import tkinter as tk

root = tk.Tk()
root.title("Styled Button - tutorialkart.com")
root.geometry("400x200")

def on_click():
    print("Styled Button Clicked!")

# Create a styled button
button = tk.Button(root, text="Styled Button", command=on_click, fg="red", font=("Arial", 14, "bold"))

button.pack(pady=20)

root.mainloop()

Screenshot in macOS

Screenshot in Windows

A styled button with red text, and bold Arial font appears.

3. Tkinter Button – With Font Style, Background Color and Text Color, etc

In the following example, we will create a Tkinter GUI application with a button. We will change the font style, width, height, background color, text color.

example.py – Python Program

</>
Copy
import tkinter as tk
import tkinter.font as font

root = tk.Tk(className='Tkinter - TutorialKart', )
root.geometry("400x200")

button_font = font.Font(family='Helvitica', size=20)

button_submit = tk.Button(root,
    text="Submit",
    bg='#45b592',
    fg='#ffffff',
    bd=0,
    font=button_font,
    height=2,
    width=15)
button_submit.pack()

root.mainloop()

Screenshot in Windows

4. Button with Variable Width and Height

Modifying the size of the button using the width and height parameters.

</>
Copy
import tkinter as tk

root = tk.Tk()
root.title("Button Size Example - tutorialkart.com")
root.geometry("400x200")

def on_click():
    print("Large Button Clicked!")

# Create a large button
button = tk.Button(root, text="Large Button", command=on_click, 
                   width=20, height=3)

button.pack(pady=20)

root.mainloop()

Screenshot in macOS

Screenshot in Windows

A large button with increased width and height is displayed.

5. Button with Image

In this example, we will display the following image instead of text on a button.

</>
Copy
import tkinter as tk
from tkinter import PhotoImage

root = tk.Tk()
root.title("Image Button Example - tutorialkart.com")
root.geometry("400x200")

def on_click():
    print("Image Button Clicked!")

# Load an image
image = PhotoImage(file="button_icon.png")  # Replace with a valid image path

# Create an image button
button = tk.Button(root, image=image, command=on_click)

button.pack(pady=20)

root.mainloop()

Screenshot in macOS

Screenshot in Windows

A button displaying an image instead of text appears.

Tkinter Button Options

Following table presents the possible options, with description and reference to example, that you can provide to a Button() constructor to alter its behavior or appearance.

OptionDescriptionExample
activebackgroundBackground color when the button is under the cursor.Tkinter Button activebackground
activeforegroundForeground color when the button is under the cursor.Tkinter Button activeforeground
anchorPosition of Text in Button.Tkinter Button anchor
bdBorder width in pixels. Default is 2.Tkinter Button bd
bgNormal background color.Tkinter Button bg
commandFunction or method to be called when the button is clicked.Tkinter Button command
fgNormal foreground (text) color.Tkinter Button fg
fontText font to be used for the button’s label.Tkinter Button font
heightHeight of the button in text lines (for textual buttons) or pixels (for images).Tkinter Button height
highlightcolorThe color of the focus highlight when the widget has focus.Tkinter Button highlightcolor
imageImage to be displayed on the button (instead of text).Tkinter Button image
justifyHow to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify.Tkinter Button justify
padxAdditional padding left and right of the text.Tkinter Button padx
padyAdditional padding above and below the text.Tkinter Button pady
reliefRelief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE.Tkinter Button relief
stateSet this option to DISABLED to gray out the button and make it unresponsive. Has the value ACTIVE when the mouse is over it. Default is NORMAL.Tkinter Button state
underlineDefault is -1, meaning that no character of the text on the button will be underlined. If nonnegative, the corresponding text character will be underlined.Tkinter Button underline
widthWidth of the button in letters (if displaying text) or pixels (if displaying an image).Tkinter Button width
wraplengthIf this value is set to a positive number, the text lines will be wrapped to fit within this length.Tkinter Button wraplength