Coordinates TK and Turtle

Peter Otten __peter__ at web.de
Tue Jan 28 06:56:15 EST 2014


duxbuz at gmail.com wrote:

> 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:

>>> print inspect.getsource(turtle._Screen._write)
    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()

>>> import turtle
>>> turtle = turtle.getturtle()
>>> turtle.goto(100, 100)
>>> turtle.write("Hello, world!")





More information about the Python-list mailing list