gpt4 book ai didi

python - 不明白此 AttributeError : '_Screen' object has no attribute 'setimage' 的原因

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:20 27 4
gpt4 key购买 nike

我的代码是:

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")

出现的错误消息是:

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'

任何建议都有帮助。

最佳答案

要做你想做的事需要一个相当复杂的解决方法(实际上是两个),因为它需要使用 tkinter 模块来完成其中的一部分(因为这就是 turtle >-graphics 模块在内部使用它来绘制图形),并且 turtle 中没有名为 setscreen() 的方法,正如您通过 发现的那样>属性错误

更复杂的是,tkinter 模块不支持 .jpg。格式图像,因此需要另一种解决方法来克服该限制,这需要还使用 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.

关于python - 不明白此 AttributeError : '_Screen' object has no attribute 'setimage' 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55173309/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com