Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

python - adding an image to the Turtle Screen

How can I add image to my Turtle Screen using turtle graphics?

whenever I use the function addshape I keep getting errors.

does turtle graphics got any other way loading/importing images?

for example:

import turtle

screen = turtle.Screen()

image = r"C:UsersmyUserDesktopPython
ocketship.png"

screen.addshape(image)
turtle.shape(image)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The turtle module does have support for images, but only GIF images, not PNG or any other format. As the docs for addshape say:

name is the name of a gif-file and shape is None: Install the corresponding image shape.

And if you look at the source, they're serious about "gif-file": the way it decides whether you're trying to add an image or a polygon is by calling data.lower().endswith(".gif"), which obviously won't work for .png files.

And, even if you fix that, it will still only be able to handle the file formats that Tkinter supports out of the box, which includes some extra things like PPM/PGM/PBM, but still not PNG. If you want to support PNG files, you'll want to install Pillow as well.

At this point, you're getting beyond what people usually do with turtle. That might be worth pursuing (you'll learn a lot by doing so), but it may be simpler to use an image-converting program to convert the .png file to a .gif file so it will work with your existing code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...