Coordinates TK and Turtle

D

duxbuz

Hello

I have some weird results when I run my code which is meant to display a canvas and a turtle and some text with the turtles coordinates.

Basically the turtle coordinates do not seem to correspond with the TK create_text coordinates.


t1.goto(100,100)

canvas_id = cv1.create_text(t1.xcor(), t1.ycor(), font=("Purisa",12),anchor="nw")

cv1.insert(canvas_id, 0, t1.pos())

I end up with this output:
http://i1025.photobucket.com/albums/y319/duxbuz/coord-issues_zps1fca6d2b.jpg

Could anyone help please?

Thanks
 
P

Peter Otten

Hello

I have some weird results when I run my code which is meant to display a
canvas and a turtle and some text with the turtles coordinates.

Basically the turtle coordinates do not seem to correspond with the TK
create_text coordinates.


t1.goto(100,100)

canvas_id = cv1.create_text(t1.xcor(), t1.ycor(),
font=("Purisa",12),anchor="nw")

cv1.insert(canvas_id, 0, t1.pos())

I end up with this output:
http://i1025.photobucket.com/albums/y319/duxbuz/coord- issues_zps1fca6d2b.jpg

Could anyone help please?

The image you provide suggests (x, y) --> (x, -y), but a quick look into the
source reveals that it is a little more complex:
def _write(self, pos, txt, align, font, pencolor):
"""Write txt at pos in canvas with specified font
and color.
Return text item and x-coord of right bottom corner
of text's bounding box."""
x, y = pos
x = x * self.xscale
y = y * self.yscale
anchor = {"left":"sw", "center":"s", "right":"se" }
item = self.cv.create_text(x-1, -y, text = txt, anchor =
anchor[align],
fill = pencolor, font = font)
x0, y0, x1, y1 = self.cv.bbox(item)
self.cv.update()
return item, x1-1

Rather than repeating the above calculation in your own code I suggest that
you use turtle.write()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top