2
import turtle
s1 = turtle.Screen()
b = turtle.Turtle()
b.shape("turtle")
b.color("blue")
b.speed(9)
s1.bgpic("grass.gif")


########## turtles
t1 = turtle.Turtle()
t1.shape('circle')
t1.pu()
t1.goto(100,200)

t2 = turtle.Turtle()
t2.shape('circle')
t2.pu()
t2.goto(-100,-200)

t3 = turtle.Turtle()
t3.shape('circle')
t3.pu()
t3.goto(200,-200)

t4 = turtle.Turtle()
t4.shape('circle')
t4.pu()
t4.goto(0,200)

t5 = turtle.Turtle()
t5.shape('circle')
t5.pu()
t5.goto(400,300)

What is the python turtle command used to change your turtle into a downloaded gif image? I need to change t1 - t5 to gif images instead of circles.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
CGA1312
  • 21
  • 7
  • Does this answer your question? [adding an image to the Turtle Screen](https://stackoverflow.com/questions/30427742/adding-an-image-to-the-turtle-screen) – ggorlen Jan 06 '23 at 17:11

2 Answers2

3

I use the following code to import images to a program:

import turtle

wn = turtle.Screen()
wn.bgcolor('white')

your_image = r"C:\Users\your_name\Pictures\your_image.gif"

wn.addshape(your_image)

To turn a turtle into a .gif image, you make the image you added their shape.

your_turtle.shape(your_image)
The Weird
  • 89
  • 1
  • 3
  • 10
0

For me, I want to use PNG, not GIF. Finally, I found the answer and recorded both GIF and PNG solution as follows, hoping to help someone.

t = turtle.Turtle()

# you can change the shape with this command, but you need to tell it what the 'your_shape_name' is.
t.shape('your_shape_name')

How to set shape

First, You need register_shape by turtle.Screen(),

Wait! Before you start, see the source code first, as below:

# Lib/turtle.py

class TurtleScreen(TurtleScreenBase):
    
    ...

    def register_shape(self, name, shape=None):
                
        if shape is None:
            # image
            if name.lower().endswith(".gif"):
                shape = Shape("image", 
                    self._image(name)  # return ``TK.PhotoImage(file=filename)`
                )
            else:
                raise TurtleGraphicsError("Bad arguments for register_shape.\n"
                                          + "Use  help(register_shape)" )
        elif isinstance(shape, tuple):
            shape = Shape("polygon", shape)
        ## else shape assumed to be Shape-instance
        self._shapes[name] = shape
        
    ...

    addshape = register_shape  # <---- so they are the same

OK, then will start,

screen = turtle.Screen()
t = turtle.Turtle()  # a Turtle instance
# t.shape('your_shape_name')  # you can change the shape with this command, but you need tell it what is 'your_shape_name'

if '1. source is png (or anything that is compatible with `tk.PhotoImage`)':    
    your_shape=turtle.Shape('image', tk.PhotoImage(file='your.png')))  # name must is 'image'   
    screen.register_shape(name='your_cool_shape_name', shape=your_shape)
    t.shape('your_cool_shape_name')
    # t.goto(1, 2)
    
if '2 source is gif':
    from pathlib import Path
    your_gif = str(Path('.../your.gif'))
    screen.register_shape(your_gif)
    t.shape(your_gif)    
Carson
  • 6,105
  • 2
  • 37
  • 45