AttributeError:'_ Screen'对象没有属性'mainloop'

时间:2015-06-14 16:50:38

标签: python python-2.7 graphics turtle-graphics

我正在设计一个简单的Python程序,它使用Turtle图形模块用箭头键在屏幕上绘制线条。

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():
   tess.forward(30)

def h2():
   tess.left(45)

def h3():
   tess.right(45)

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.
wn.listen()
wn.mainloop()

当我尝试执行它时,会返回以下错误。

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

我正在使用Python 2.7并且之前没有遇到过Turtle图形的问题。直到现在我才开始使用关键输入来解决这个问题。

在网上搜索,我只找到了与我目前遇到的不同问题和模块的文章。

如果您需要更多信息,请与我们联系。谢谢!

1 个答案:

答案 0 :(得分:2)

它仍然是turtle.mainloop(),而不是wn.mainloop()

我怀疑这是因为你可以创建多个屏幕,所以仍然让turtle模块管理所有屏幕而不是尝试使多个主循环协同工作是有意义的。