简体   繁体   English

不了解此AttributeError的原因:“ _ Screen”对象没有属性“ setimage”

[英]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.

相关问题 不明白这个 AttributeError 的原因:&#39;list&#39; 对象没有属性 &#39;split&#39; - Don't understand cause of this AttributeError: 'list' object has no attribute 'split' 不明白这个 AttributeError 的原因:'NoneType' object has no attribute 'find_all' - Don't understand the cause of this AttributeError: 'NoneType' object has no attribute 'find_all' 不了解此AttributeError:“ str”对象没有属性“ text” - Don't understand this AttributeError:'str' object has no attribute 'text' 不了解此AttributeError:“函数”对象没有属性“ isalpha” - Don't understand this AttributeError: 'function' object has no attribute 'isalpha' 不了解此AttributeError的原因 - Don't understand cause of this AttributeError 不了解此AttributeError的原因 - Don't understand the cause of this AttributeError 不了解此pytz AttributeError:&#39;UTC&#39;对象没有属性&#39;utc_timezone&#39; - Don't understand this pytz AttributeError: 'UTC' object has no attribute 'utc_timezone' 不明白为什么这个AttributeError:&#39;Graph&#39;对象没有属性&#39;merge_one&#39;正在发生 - Don't understand why this AttributeError: 'Graph' object has no attribute 'merge_one' is occurring AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; ,我不明白如何修复它 - AttributeError: 'NoneType' object has no attribute 'text' , I don't understand how to fix it 不知道为什么:AttributeError: &#39;list&#39; 对象没有属性 &#39;groupby&#39; - don't know why: AttributeError: 'list' object has no attribute 'groupby'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM