How do I get the dimensions of Python's turtle in pixels?
The turtle shapes are stored in a dictionary in the TurtleScreen class where the key for shape square, for example, contains:
Shape("polygon", ((10,-10), (10,10), (-10,10), (-10,-10)))
All of the cursor shapes appear to fit within this 20 x 20 square. What I do when I program with shapesize() is the following:
STAMP_SIZE = 20
...
turtle.shapesize(size / STAMP_SIZE)
Where size is how large (or small) I want the cursor to be in pixels. This doesn't have to be uniform, sometimes I make lines by doing:
turtle.shapesize(size / STAMP_SIZE, 1 / STAMP_SIZE) # vertical line
Remember that the width and length arguments to shapesize() are relative to the turtle's orientation so width is in the direction the turtle is pointing and length is perpendicular to that.
If you really want to know the dimensions of the current turtle cursor (including after any transformations), you can use turtle.get_shapepoly(). But this gives you a polygon so you'll need to figure out height, width, area or what it is you need from the polygon verticies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With