How can I import an image in Python Turtle?

2020-04-21 07:03发布

问题:

What I want, is to get an image to replace the default turtle, just like it is done here: http://blog.trinket.io/using-images-in-turtle-programs/ .

This is my code, but I don't know why it doesn't work:

import turtle

image = "C:/Python27/Pythonprogramming/image.png"
screen = turtle.Screen()

screen.addshape(image)
turtle.shape(image)

turtle.mainloop()

This is all in effort to make a turtle that's actions can be manipulated through Python code. I have searched all over the internet, but can't seem to find a solution.

Thanks in advance for any help!

回答1:

I'm not sure but I think on Windows ,the paths will be like this: "C:\Python2\Pythonprogramming\image.png" Also , what error does it raise exactly?



回答2:

The turtle graphics has support for .gif images only. Please try to change your image's format.

Here's a link to a similar question How can i add an image (Python)



回答3:

You can take screenshot of the pic and can convert/resize to .gif at ezgif.com.

Background:

win = turtle.Screen()
win.bgpic('background.gif') or whatever the file name in the directory.

Using Shapes:

win.register_shape('pic1.gif')
rocket = turtle.Turtle()
rocket.shape('pic1.gif')

Simple example game:

https://www.youtube.com/watch?v=sRnSJvSHgBs&t=4s



回答4:

Trinket is a program for python! You have to type your code in trinket if you want that code to work!