Python 属性错误:'_屏幕';对象没有属性';主回路';
我正在设计一个简单的Python程序,它使用Turtle图形模块在屏幕上用箭头键画线Python 属性错误:'_屏幕';对象没有属性';主回路';,python,python-2.7,graphics,turtle-graphics,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 t
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'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Noah Huber Feely\Desktop\PEN\u Programming\Python\etchy\u sketch1.py”,第32行,在
wn.mainloop()
AttributeError:“\u Screen”对象没有属性“mainloop”
我使用的是Python2.7,以前在海龟图形方面没有遇到过问题。直到现在,我才开始处理关键输入,这个问题才出现了
在网上搜索时,我只找到了与我目前遇到的问题和模块不同的文章
如果你需要更多信息,请告诉我。谢谢 它仍然是
turtle.mainloop()
,而不是wn.mainloop()
我怀疑这是因为您可以创建多个屏幕,所以仍然让turtle
模块管理所有屏幕,而不是试图让多个主循环一起工作是有意义的。wn.mainloop()
仅在Python 3中有效。在Python 2中,mainloop
是TK.mainloop
的(全局模块)同义词。在Python3中,mainloop
是调用TK.mainloop
的TurtleScreenBase
方法。当像这样使用turtle standalone(即不嵌入Tk)时,只有一个屏幕,没有办法制作其他屏幕。