gpt4 book ai didi

python - 属性错误 : '_Screen' object has no attribute 'mainloop'

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:53 31 4
gpt4 key购买 nike

我正在设计一个简单的 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 图形时没有遇到任何问题。直到现在我开始使用键输入时才出现此问题。

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

如果您需要更多信息,请告诉我。谢谢!

最佳答案

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

我怀疑这样做的原因是因为您可以制作多个屏幕,所以仍然让 turtle 模块管理所有屏幕而不是试图潜在地让多个主循环一起工作是有意义的。

关于python - 属性错误 : '_Screen' object has no attribute 'mainloop' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30832015/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com