Python Turtle Size: How to Set It
In this article
The size of a Python turtle is controlled by the shapesize() method (also aliased as turtlesize()), which scales the turtle's default shape (e.g., 'triangle', 'circle') in pixels. By default, a turtle is 20 pixels wide and 20 pixels tall, but you can stretch it horizontally, vertically, or both using turtle.shapesize(stretch_wid, stretch_len, outline).
What Determines a Turtle's Size?
In Python's turtle module, every turtle has a shape (like 'arrow', 'turtle', 'circle', 'square', 'triangle', or 'classic'). The size of that shape is measured in pixels, and the default dimensions are 20×20 pixels for most shapes. The shapesize() method takes three optional arguments:
stretch_wid– vertical stretch factor (multiplies the width, i.e., the horizontal size).stretch_len– horizontal stretch factor (multiplies the length, i.e., the vertical size).outline– width of the shape's outline (default is 1).
For example, turtle.shapesize(2, 3) makes the turtle 40 pixels wide and 60 pixels tall (20×2 and 20×3). If you call shapesize() with no arguments, it returns the current size factors as a tuple.
How to Set the Turtle Size in Code
To change a turtle's size, you first create a turtle object and then call shapesize() on it. Here's a minimal example:
import turtle
t = turtle.Turtle()
t.shapesize(2, 2) # double the default size
You can also use turtlesize() as an alias. Both work identically. The stretch factors are floating-point numbers, so you can make a turtle slightly larger (e.g., 1.5) or much larger (e.g., 5).
Choosing the Right Size for Your Project
The ideal turtle size depends on your screen and the visual effect you want. Here are practical guidelines:
- Default (20×20 px): Good for most simple drawings and when you need precise positioning.
- Larger (e.g., 2× or 3×): Useful for making a turtle a visible icon, like in a game or when demonstrating movement.
- Smaller (e.g., 0.5×): Useful when you want the turtle to be unobtrusive while drawing.
Remember that the turtle's size does not affect the lines it draws – only the shape that represents the cursor. If you want thicker lines, use pensize() instead.
Common Problems and Misconceptions
One frequent mistake is confusing shapesize() with pensize(). shapesize() changes the turtle icon; pensize() changes the width of the drawn line. Another issue is that shapesize() does not accept a single number to scale uniformly – you must pass two arguments. For example, shapesize(2) sets the vertical stretch to 2 but leaves horizontal at 1, making a tall, narrow turtle. To scale uniformly, pass the same value twice.
Also, if you use a custom shape (via register_shape()), the size is based on the shape's original pixel dimensions. The stretch factors still apply, but the default size may not be 20×20.
Comparing Turtle Sizes Across Python Versions
The turtle module has been part of Python's standard library since Python 2.5, and the shapesize() method has remained consistent. However, in Python 2, the method was called turtlesize(); in Python 3, both names work. The default shape size has always been 20×20 pixels, but the 'classic' shape (the arrow) is actually 20×20 as well. No significant changes have been made to size handling in recent Python versions.
Performance and Screen Size Considerations
Making a turtle very large (e.g., 10×10) can cause it to cover a large portion of the screen, which might obscure your drawing. It can also slow down rendering slightly because the shape must be redrawn each time the turtle moves. For most projects, a stretch factor between 0.5 and 3 is sufficient. If you need a truly huge icon, consider using a custom shape or a stamp instead of resizing the turtle.
Alternatives to Changing Turtle Size
If you want a different visual appearance without altering the turtle's size, you can:
- Change the shape: Use
t.shape('circle')or another built-in shape. - Use stamps:
t.stamp()leaves a copy of the turtle's shape at its current position, and you can resize the stamp by setting the turtle's size before stamping. - Hide the turtle:
t.hideturtle()makes the turtle invisible while drawing, which is useful for final artwork.
Frequently Asked Questions
How do I make the turtle bigger in Python?
Call turtle.shapesize(stretch_wid, stretch_len) on your turtle object. For example, t.shapesize(3, 3) makes it three times the default size in both directions. You can also use turtlesize().
What is the default size of a turtle?
The default size is 20 pixels wide and 20 pixels tall for all built-in shapes. This is the base size before any stretching is applied.
Does changing the turtle size affect the lines it draws?
No. The turtle's size only affects the on-screen icon. The width of the drawn line is controlled by pensize(), which is independent.
Can I set the turtle size to a specific pixel value?
Yes, by calculating the stretch factor. For example, to make a turtle 50 pixels wide, divide 50 by 20 to get 2.5, then call shapesize(2.5, 2.5) for uniform scaling.
Why does my turtle look stretched when I use shapesize(2)?
Because shapesize(2) sets only the first argument (vertical stretch) to 2, leaving the horizontal at 1. This makes the turtle twice as tall but the same width. Pass two equal numbers for uniform scaling.