使用Tu后如何关闭窗口

2024-06-26 02:59:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用海龟在Spyder和Jupyter,但我有麻烦,当我试图关闭窗口。你知道吗

我在spyder3.3.1上使用python3.7,在windows10上使用jupyter5.6.0

这就是我尝试过的

import turtle as trtl

trtl.forward(100)
trtl.left(90)
trtl.forward(100)
trtl.left(90)
trtl.forward(100)
trtl.left(90)
trtl.forward(100)
trtl.exitonclick()

我工作正常,但如果关闭窗口并再次尝试运行相同的代码,则会出现以下错误:

---------------------------------------------------------------------------
Terminator                                Traceback (most recent call last)
<ipython-input-9-ad2d84897daf> in <module>()
      1 import turtle as trtl
      2 
----> 3 trtl.forward(100)
      4 trtl.left(90)
      5 trtl.forward(100)

~\Anaconda3\lib\turtle.py in forward(distance)

Terminator: 

我尝试了done()bye()exitonclick()的多种组合,但都无法成功


Tags: 代码inimportasjupyterleftforward海龟
2条回答

以下是海龟documentation的节选:

If the value “using_IDLE” in the configuration dictionary is False (default value), also enter mainloop. Remark: If IDLE with the -n switch (no subprocess) is used, this value should be set to True in turtle.cfg. In this case IDLE’s own mainloop is active also for the client script.

因此,您可以将using_IDLE = True添加到^{}文件来解决这个问题。这将阻止exitonclick()进入主循环。你知道吗

有点晚了,但我认为在Jupyter解决这个问题的最佳选择是:

import importlib
import turtle
importlib.reload(turtle)

turtle.forward(1)
turtle.exitonclick()

问题是TK应用程序是在import turtle中创建的,当您在窗口中单击时进入bye时终止。当您尝试在之后执行一些turtle指令时,TK应用程序被终止,然后发生错误。所以重新加载海龟包就解决了这个问题

相关问题 更多 >