【问题标题】:Don't understand cause of this AttributeError: '_Screen' object has no attribute 'setimage'不了解此 AttributeError 的原因:“_Screen”对象没有属性“setimage”
【发布时间】:2019-08-05 23:38:04
【问题描述】:

我的代码是:

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'

任何建议都是有帮助的。

【问题讨论】:

    标签: python image turtle-graphics appjar


    【解决方案1】:

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

    更复杂的是,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.
    

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 2021-12-08
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2018-09-10
      • 2012-12-01
      • 2021-04-19
      • 2019-08-11
      相关资源
      最近更新 更多