我导入turtle时总是出错

-1 投票
3 回答
16056 浏览
提问于 2025-04-17 23:52
import turtle
window = turtle.screen()
myTurtle.forward(100)
myTurtle.left(90)
myTurtle.forward(100)

window.mainloop()

我在尝试使用上面的代码时遇到了这个错误,不知道为什么,因为这和我老师的幻灯片是一样的,我只是想自己测试一下。

Traceback (most recent call last):
File "/Users/ruairimangan-cliff/Desktop/Foundations of Computer Programming/week 4/Week 4 'Functions'.py", line 72, in <module>
window = turtle.screen()
AttributeError: 'module' object has no attribute 'screen'

3 个回答

4

另外,如果你遇到同样的问题,但代码里没有拼写错误:

window = turtle.Screen()

但是你仍然收到了错误信息:

AttributeError: 'module' object has no attribute 'Screen'

你的脚本是不是叫做 turtle.py?

如果是的话,它会优先于标准库中的 turtle 模块。

你可以按照 这个回答 的建议来重命名你的脚本。

7

我也遇到过这个问题。原来是因为我把文件命名为turtle.py,这个名字和一个模块冲突了。一旦我把文件名改成其他的,它就开始正常工作了。

4

确保你仔细阅读你的错误信息!一个小小的拼写错误或者大小写写错了,都可能导致这样的错误出现!

把:

window = turtle.screen()

改成:

window = turtle.Screen()

http://openbookproject.net/thinkcs/python/english3e/hello_little_turtles.html

撰写回答