Noah Huber-Feely Noah Huber-Feely - 1 year ago 178
Python Question

AttributeError: '_Screen' object has no attribute 'mainloop'

I'm designing a simple Python program that uses the Turtle graphics module to draw lines on the screen with the arrow keys.

import turtle

turtle.setup(400,500) # Determine the window size
wn = turtle.Screen() # Get a reference to the window
wn.title("Handling keypresses!") # Change the window title
wn.bgcolor("lightgreen") # Set the background color
tess = turtle.Turtle() # Create our favorite turtle

# The next four functions are our "event handlers".
def h1():

def h2():

def h3():

def h4():
wn.bye() # Close down the turtle window

# These lines "wire up" keypresses to the handlers we've defined.
wn.onkey(h1, "Up")
wn.onkey(h2, "Left")
wn.onkey(h3, "Right")
wn.onkey(h4, "q")

# Now we need to tell the window to start listening for events,
# If any of the keys that we're monitoring is pressed, its
# handler will be called.

When I try to execute it, the following error is returned.

Traceback (most recent call last):
File "C:\Users\Noah Huber-Feely\Desktop\PEN_Programming\Python\", line 32, in <module>
AttributeError: '_Screen' object has no attribute 'mainloop'

I'm using Python 2.7 and have had no trouble with Turtle graphics before. It is only now that I have begun working with key input that this problem has occurred.

Searching on the web, I only found articles about different issues and modules than the one I am currently experiencing.

Let me know if you need more information. Thanks!

Answer Source

It's still turtle.mainloop(), not wn.mainloop().

I suspect the reason for this is since you can make multiple screens, it makes sense to still have the turtle module manage all of the screens instead of trying to potentially make multiple mainloops work together.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download