How to get the coordinates of an object in a Tkinter canvas?

The Tkinter Canvas widget provides GUI features for drawing and managing graphical objects. When creating shapes, you specify their size and coordinates in the constructor. To retrieve the coordinates of any canvas item later, use the coords(item) method, which returns a list containing the object's coordinate values.

Syntax

canvas.coords(item_id)

Parameters

  • item_id − The ID of the canvas item (returned when creating the item)

Return Value

Returns a list of coordinates. For rectangles and ovals, this contains [x1, y1, x2, y2] representing the bounding box.

Example

Here's how to create a canvas item and retrieve its coordinates ?

from tkinter import *

# Create an instance of tkinter frame
win = Tk()
win.geometry("700x250")

# Initialize a Canvas Object
canvas = Canvas(win, width=500, height=300)

# Draw an oval inside canvas object
oval_id = canvas.create_oval(100, 10, 410, 200, outline="red", fill="#adf123")
canvas.pack(expand=True, fill=BOTH)

# Get and Print the coordinates of the Oval
coords = canvas.coords(oval_id)
print("Coordinates of the oval are:", coords)

win.mainloop()

The output of the above code is ?

Coordinates of the oval are: [100.0, 10.0, 410.0, 200.0]

Getting Coordinates of Multiple Objects

You can store multiple item IDs and retrieve their coordinates individually ?

from tkinter import *

win = Tk()
win.geometry("600x400")

canvas = Canvas(win, width=500, height=350, bg="white")

# Create multiple shapes and store their IDs
rectangle_id = canvas.create_rectangle(50, 50, 200, 150, fill="blue")
circle_id = canvas.create_oval(250, 100, 400, 250, fill="green")
line_id = canvas.create_line(100, 200, 300, 300, width=3, fill="red")

canvas.pack()

# Get coordinates of all objects
print("Rectangle coordinates:", canvas.coords(rectangle_id))
print("Circle coordinates:", canvas.coords(circle_id))
print("Line coordinates:", canvas.coords(line_id))

win.mainloop()

The output shows coordinates for each shape ?

Rectangle coordinates: [50.0, 50.0, 200.0, 150.0]
Circle coordinates: [250.0, 100.0, 400.0, 250.0]
Line coordinates: [100.0, 200.0, 300.0, 300.0]

Key Points

  • The coords() method returns coordinates as floating-point numbers
  • For rectangles and ovals: [x1, y1, x2, y2] (top-left and bottom-right corners)
  • For lines: [x1, y1, x2, y2, ...] (start and end points, plus any intermediate points)
  • You can also use coords() to modify coordinates by passing new values

Conclusion

Use canvas.coords(item_id) to retrieve the coordinates of any canvas object. This method returns a list of coordinate values that define the object's position and size on the canvas.

Updated on: 2026-03-25T20:43:40+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements