[英]Don't understand cause of this AttributeError: '_Screen' object has no attribute 'setimage'
The code I have is: 我的代码是:
import time
import turtle
from turtle import *
from random import randint
#GUI options
screen = turtle.Screen()
screen.setup(1000,1000)
screen.setimage("eightLane.jpg")
title("RACING TURTLES")
The error message that comes up is: 出现的错误消息是:
Traceback (most recent call last): File
"/Users/bradley/Desktop/SDD/coding term 1 year 11/8 lane
experementaiton.py", line 14, in <module>
screen.setimage("eightLane.jpg") AttributeError: '_Screen' object has no attribute 'setimage'
Any advice is helpful. 任何建议都是有帮助的。
To do what you want requires a fairly involved workaround (two of them actually) because it requires using the tkinter
module to do part of it (because that's what the turtle
-graphics module uses internally to do its graphics), and turtle
doesn't have a method in it called setscreen()
, as you've discovered via the AttributeError
. 要执行您想要的操作,需要一个相当复杂的解决方法(实际上有两个),因为它需要使用
tkinter
模块来完成其中的一部分(因为这是turtle
-graphics模块内部使用的方式来制作其图形),而turtle
没有正如您通过AttributeError
发现的那样,其中有一个名为setscreen()
的方法。
The complicate matters further, the tkinter
module doesn't support .jpg
. 更复杂的是,
tkinter
模块不支持.jpg
。 format images, so yet another workaround is needed to overcome that limitation, which requires needing to also use the PIL
(Python Imaging Library) to convert the image into a format tkinter
does support. 格式化图像,因此还需要另一种解决方法来克服该限制,即还需要使用
PIL
(Python图像库)将图像转换为tkinter
支持的格式。
from PIL import Image, ImageTk
from turtle import *
import turtle
# GUI options
screen = turtle.Screen()
screen.setup(1000, 1000)
pil_img = Image.open("eightLane.jpg") # Use PIL to open .jpg image.
tk_img = ImageTk.PhotoImage(pil_img) # Convert it into something tkinter can use.
canvas = turtle.getcanvas() # Get the tkinter Canvas of this TurtleScreen.
# Create a Canvas image object holding the tkinter image.
img_obj_id = canvas.create_image(0, 0, image=tk_img, anchor='center')
title("RACING TURTLES")
input('press Enter') # Pause before continuing.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.