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 to center an image in canvas Python Tkinter
Let us consider that we are creating a GUI-based application using Tkinter and we want to load an image in the Tkinter canvas. By default, the canvas loads the images according to its width and height. However, we can manipulate the position of an image in any direction by passing the direction value in the anchor parameter.
An anchor is a parameter which is invoked along with the image function; it defines the direction or position of the image in the canvas. By using anchor parameters, we can align the text and images in any direction.
Method 1: Using Label with Anchor
We can create an image label using the Label function and adjust its position using the anchor property. Since we have to place the image at the center, we will pass the value of anchor as CENTER.
Example
# Import the tkinter library
from tkinter import *
# Creating an instance of the tkinter window
win = Tk()
win.geometry("700x400")
win.title("Centered Image Example")
# Define the image label with center alignment
label_img = Label(
win,
text="Hello World",
font="Arial 16",
relief="solid",
width=20,
height=8,
anchor=CENTER,
bg="lightblue"
)
# Pack the label to center it in the window
label_img.pack(expand=True)
# Display the window
win.mainloop()
Method 2: Using Canvas with Actual Image
For actual image centering in a Canvas widget, we can use the create_image() method with calculated center coordinates ?
from tkinter import *
# Create main window
root = Tk()
root.geometry("600x400")
root.title("Centered Image in Canvas")
# Create canvas
canvas = Canvas(root, width=600, height=400, bg="white")
canvas.pack()
# Create a simple rectangle as image placeholder
# In real scenario, use PhotoImage to load actual image
canvas_width = 600
canvas_height = 400
# Calculate center coordinates
center_x = canvas_width // 2
center_y = canvas_height // 2
# Create centered rectangle (representing an image)
rect = canvas.create_rectangle(
center_x - 100, center_y - 50, # x1, y1
center_x + 100, center_y + 50, # x2, y2
fill="lightgreen",
outline="black",
width=2
)
# Add text in center
canvas.create_text(center_x, center_y, text="Centered Image", font="Arial 14")
root.mainloop()
Method 3: Using PhotoImage for Real Images
Here's how to center an actual image file in a Canvas ?
from tkinter import *
root = Tk()
root.geometry("600x400")
root.title("Centered Photo")
# Create canvas
canvas = Canvas(root, width=600, height=400, bg="gray90")
canvas.pack()
# Load image (replace with your image path)
try:
# For demonstration, we'll create a simple colored rectangle
# In practice: img = PhotoImage(file="your_image.png")
canvas_width = 600
canvas_height = 400
# Calculate center
center_x = canvas_width // 2
center_y = canvas_height // 2
# Place image at center using anchor
# canvas.create_image(center_x, center_y, image=img, anchor=CENTER)
# For demo, create centered text
canvas.create_text(center_x, center_y,
text="Image would be\ncentered here",
font="Arial 16",
justify=CENTER)
except Exception as e:
print(f"Error loading image: {e}")
root.mainloop()
Anchor Options
The anchor parameter accepts these values ?
| Anchor Value | Position | Description |
|---|---|---|
N |
North | Top center |
S |
South | Bottom center |
E |
East | Right center |
W |
West | Left center |
CENTER |
Center | Exact center |
Conclusion
Use anchor=CENTER with Label widgets for simple centering, or calculate center coordinates for Canvas widgets. The create_image() method with anchor=CENTER provides precise image positioning in Canvas.
