Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating a button in tkinter
is one of the fundamental tasks. Here's a basic tutorial on how to create a button:
1. Import Required Libraries:
import tkinter as tk
2. Create the Main Application Window:
root = tk.Tk() root.title("Button Tutorial")
3. Define the Function to be Called upon Button Click: Before creating the button, let's create a simple function that will be called when the button is clicked:
def on_button_click(): print("Button was clicked!")
4. Create the Button:
Now, let's create a button and set its text as "Click Me". We'll also bind the above function to this button using the command
parameter:
button = tk.Button(root, text="Click Me", command=on_button_click)
5. Display the Button on the Window:
The button will not appear until you use a geometry manager (like pack
, grid
, or place
) to add it to the window. For simplicity, we'll use the pack
method:
button.pack(pady=20)
Here, pady=20
provides some vertical padding to center the button a bit.
6. Run the Main Loop: This will keep the window running and responsive:
root.mainloop()
Complete Code:
import tkinter as tk def on_button_click(): print("Button was clicked!") root = tk.Tk() root.title("Button Tutorial") button = tk.Button(root, text="Click Me", command=on_button_click) button.pack(pady=20) root.mainloop()
Run the complete code, and a simple GUI window will appear with a button. When you click the button, "Button was clicked!" will be printed to the console.
You can further customize the button by adjusting its properties such as bg
(background color), fg
(foreground/text color), font
(font and size), and many more.
Python Tkinter create button example:
import tkinter as tk root = tk.Tk() button = tk.Button(root, text="Click me") button.pack() root.mainloop()
How to make a button in Tkinter:
import tkinter as tk root = tk.Tk() button = tk.Button(root, text="Click me") button.pack() root.mainloop()
Creating a simple button in Tkinter:
import tkinter as tk root = tk.Tk() button = tk.Button(root, text="Click me") button.pack() root.mainloop()
Adding a button to Tkinter window:
import tkinter as tk def on_button_click(): print("Button clicked!") root = tk.Tk() button = tk.Button(root, text="Click me", command=on_button_click) button.pack() root.mainloop()
Tkinter button command and options:
import tkinter as tk def on_button_click(): print("Button clicked!") root = tk.Tk() button = tk.Button(root, text="Click me", command=on_button_click, bd=5, bg="lightblue") button.pack() root.mainloop()
Python Tkinter button click event:
import tkinter as tk def on_button_click(): print("Button clicked!") root = tk.Tk() button = tk.Button(root, text="Click me", command=on_button_click) button.pack() root.mainloop()
Customizing Tkinter button appearance:
import tkinter as tk def on_button_click(): print("Button clicked!") root = tk.Tk() button = tk.Button(root, text="Click me", command=on_button_click, bg="green", fg="white", font=("Arial", 12)) button.pack() root.mainloop()
Tkinter button size and placement:
import tkinter as tk def on_button_click(): print("Button clicked!") root = tk.Tk() button = tk.Button(root, text="Click me", command=on_button_click, width=10, height=2) button.pack(pady=20, padx=50) root.mainloop()