Brad
Brad

Reputation: 21

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.

Upvotes: 1

Views: 821

Answers (1)

martineau
martineau

Reputation: 123473

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.

The complicate matters further, the tkinter module doesn't support .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.

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.

Upvotes: 0

Related Questions