Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I can't seem to figure out how to retrieve the x,y position of an oval created on a Tkinter canvas using Python via

c.create_oval(x0, y0, x1, y2) 

I understand that Tkinter creates the oval inside the box specified by x0,y0,x1,y2 and if I can get those coordinates that would also work.

I need the coordinates to move the oval by an offset equal to the mouse coords and the actual oval.

like image 796
Omar Avatar asked Apr 20 '10 23:04

Omar


People also ask

What does Canvas Coords return?

In most cases it will be a 4-tuple (x1 , y1 , x2 , y2 ) describing the bounding box of the object . The rule of thumb is that if it takes two coordinates to define the object (text in your case), then that is the number of coordinates the command will return.

What are the Canvas coordinates?

The coordinate space of the canvas has 0, 0 at the top left corner. Larger X coordinates are to the right, and larger Y coordinates are downward. The position and possibly the size of a canvas object is determined by a set of coordinates.

How do I change the position of a Canvas in Python?

You can use . moveto(item_id, x, y) to move the canvas item to absolute position (x, y) .

What is difference between frame and Canvas in tkinter?

A Frame is designed to be a parent container of other widgets. A Canvas is like a canvas that you can draw somethings on it like lines, circles, text, etc. A Canvas can be used as a parent container as well but it is not designed for that at the first place.


1 Answers

Assign the results of c.create_oval to x -- that's the "object ID" of the oval. Then,

c.coords(x) 

gives you the (x1, y1, x2, y2) tuple of the oval's coordinates (you call coords with new coordinates following the x to move the oval).

like image 60
Alex Martelli Avatar answered Nov 28 '22 09:11

Alex Martelli