海龟模块错误

时间:2018-08-27 09:53:08

标签: python turtle-graphics

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"

如果有什么可以帮助我指出我做错了的事情,将不胜感激。

1 个答案:

答案 0 :(得分:0)

您只是按错误的顺序排列了行:

import turtle

bob = turtle.Turtle()
print(bob)
bob.fd(1000)

turtle.mainloop()

最后的turtle.mainloop()行将控制权移交给tkinter的事件循环,因此之后的任何行直到窗口关闭且乌龟环境被拆除之前都不会执行。即这通常是您在乌龟程序中要做的最后一件事。