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
| Parameter | Type | Description |
|---|---|---|
master | Widget | The parent widget, usually a frame or root window. |
text | String | The text displayed on the button. |
command | Function | The function to execute when the button is clicked. |
**options | Various | Additional 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()
