Turtle 模块错误
Turtle Module Error
这里是 python 的新手。我正在尝试了解 python 中的 turtle 模块,但我在设置它时遇到了问题。这是我正在尝试的脚本 运行:
import turtle
bob = turtle.Turtle()
print(bob)
turtle.mainloop()
bob.fd(1000)
这是我得到的错误:
Traceback (most recent call last):
File "turtle_test.py", line 6, in <module>
bob.fd(1000)
File "/usr/lib/python3.6/turtle.py", line 1637, in forward
self._go(distance)
File "/usr/lib/python3.6/turtle.py", line 1605, in _go
self._goto(ende)
File "/usr/lib/python3.6/turtle.py", line 3158, in _goto
screen._pointlist(self.currentLineItem),
File "/usr/lib/python3.6/turtle.py", line 755, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "/usr/lib/python3.6/tkinter/__init__.py", line 2466, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"
如果有任何人可以帮助我并指出我做错了什么,将不胜感激。
您只是顺序错误:
import turtle
bob = turtle.Turtle()
print(bob)
bob.fd(1000)
turtle.mainloop()
最后的 turtle.mainloop()
行将控制权交给 tkinter 的事件循环,因此在 window 关闭并且 turtle 环境被拆除之前,不会执行此后的任何行。 IE。这通常是您在 turtle 程序中做的最后一件事。
这里是 python 的新手。我正在尝试了解 python 中的 turtle 模块,但我在设置它时遇到了问题。这是我正在尝试的脚本 运行:
import turtle
bob = turtle.Turtle()
print(bob)
turtle.mainloop()
bob.fd(1000)
这是我得到的错误:
Traceback (most recent call last):
File "turtle_test.py", line 6, in <module>
bob.fd(1000)
File "/usr/lib/python3.6/turtle.py", line 1637, in forward
self._go(distance)
File "/usr/lib/python3.6/turtle.py", line 1605, in _go
self._goto(ende)
File "/usr/lib/python3.6/turtle.py", line 3158, in _goto
screen._pointlist(self.currentLineItem),
File "/usr/lib/python3.6/turtle.py", line 755, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "/usr/lib/python3.6/tkinter/__init__.py", line 2466, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"
如果有任何人可以帮助我并指出我做错了什么,将不胜感激。
您只是顺序错误:
import turtle
bob = turtle.Turtle()
print(bob)
bob.fd(1000)
turtle.mainloop()
最后的 turtle.mainloop()
行将控制权交给 tkinter 的事件循环,因此在 window 关闭并且 turtle 环境被拆除之前,不会执行此后的任何行。 IE。这通常是您在 turtle 程序中做的最后一件事。