【发布时间】:2018-11-12 06:24:12
【问题描述】:
当我在 Turtle 图形中运行无限循环绘图时单击 X(关闭)按钮时,会出现一些错误消息。
这是一个例子:
import turtle
wn = turtle.Screen()
tess = turtle.Turtle()
while True:
tess.forward(50)
tess.left(120)
tess.forward(50)
wn.mainloop()
当我关闭窗口时,会显示以下错误消息。
Traceback (most recent call last):
File "/Users/user/Downloads/test.py", line 8, in <module>
tess.forward(50)
File "/Users/user/anaconda3/lib/python3.6/turtle.py", line 1637, in forward
self._go(distance)
File "/Users/user/anaconda3/lib/python3.6/turtle.py", line 1605, in _go
self._goto(ende)
File "/Users/user/anaconda3/lib/python3.6/turtle.py", line 3178, in _goto
self._pencolor, self._pensize, top)
File "/Users/user/anaconda3/lib/python3.6/turtle.py", line 545, in _drawline
self.cv.coords(lineitem, *cl)
File "<string>", line 1, in coords
File "/Users/user/anaconda3/lib/python3.6/tkinter/__init__.py", line 2463, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"
我想知道如何避免此类错误消息。
有什么方法可以像使用 tkinter 模块中的 Tk 类的“WM_DELETE_WINDOW”选项的“协议”方法一样吗?
【问题讨论】:
-
您是否知道
wn.mainloop()永远无法运行,因为您的无限循环在那之前? -
是的,这就是我的怀疑。下面的答案很好地解释了我想知道的。