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