Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do you create a Button on a Tkinter Canvas?
Canvas widget is one of the versatile widgets in the Tkinter library. You can use canvas to draw different shapes, arcs, and objects to animate within the canvas. To create a button on a Tkinter Canvas, you can either pass the canvas as the parent to the Button constructor or use the create_window() method for more precise positioning.
Method 1: Using Button with Canvas as Parent
In this approach, we create a button widget with the canvas as its parent ?
import tkinter as tk
from tkinter import ttk
# Create an instance of Tkinter window
root = tk.Tk()
root.geometry("400x300")
root.title("Button on Canvas")
# Define a function for the button
def button_click():
print("Button clicked!")
# Create a canvas widget
canvas = tk.Canvas(root, width=350, height=250, bg="lightgray")
# Add a label in the canvas
label = tk.Label(canvas, text="Click the Button Below", font=('Arial', 14, 'bold'))
label.pack(pady=20)
# Create a button in canvas widget
button = ttk.Button(canvas, text="Click Me", command=button_click)
button.pack(pady=10)
canvas.pack(pady=20)
root.mainloop()
Method 2: Using create_window() Method
The create_window() method provides more control over button positioning on the canvas ?
import tkinter as tk
from tkinter import ttk
# Create main window
root = tk.Tk()
root.geometry("400x300")
root.title("Button with create_window")
def on_button_click():
print("Button at coordinates clicked!")
# Create canvas
canvas = tk.Canvas(root, width=350, height=250, bg="white", relief="solid", bd=1)
# Create button widget
button = ttk.Button(root, text="Positioned Button", command=on_button_click)
# Add button to canvas at specific coordinates
canvas.create_window(175, 125, window=button)
# Add some canvas elements for context
canvas.create_rectangle(50, 50, 300, 200, outline="blue", width=2)
canvas.create_text(175, 75, text="Canvas with Button", font=('Arial', 12, 'bold'))
canvas.pack(pady=20)
root.mainloop()
Complete Example with Exit Functionality
Here's a complete example that demonstrates creating a button on canvas with exit functionality ?
import tkinter as tk
from tkinter import ttk
# Create main window
root = tk.Tk()
root.geometry("400x300")
root.title("Canvas Button Example")
# Function to exit the program
def exit_program():
root.destroy()
# Create canvas
canvas = tk.Canvas(root, width=350, height=250, bg="lightblue")
# Add title text on canvas
canvas.create_text(175, 50, text="Canvas with Button Demo",
font=('Arial', 16, 'bold'), fill="darkblue")
# Create and position exit button
exit_button = ttk.Button(root, text="Exit Program", command=exit_program)
canvas.create_window(175, 150, window=exit_button)
# Add decorative elements
canvas.create_oval(100, 100, 250, 200, outline="red", width=3, fill="lightyellow")
canvas.pack(pady=20)
root.mainloop()
Key Differences
| Method | Positioning | Best For |
|---|---|---|
| Canvas as Parent | Uses pack/grid | Simple layouts |
| create_window() | Exact coordinates | Precise positioning |
Conclusion
Use canvas as the button parent for simple layouts with pack/grid. Use create_window() method when you need precise positioning control on the canvas.
