Python Turtle 窗口每运行 2 次就会崩溃一次 [英] Python Turtle window crashes every 2nd time running

查看:100
本文介绍了Python Turtle 窗口每运行 2 次就会崩溃一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是在python中使用Turtle绘制的基本正方形.

The code below is a basic square drawing using Turtle in python.

第一次运行代码有效.但是再次运行代码会激活一个无响应的 Turtle 窗口,随后每次都会崩溃.

Running the code the first time works. But running the code again activates a Turtle window that is non-responsive and subsequently crashes every time.

错误信息包括raise TerminatorTerminator

在 Spyder 中重新启动内核(戴尔台式机上的 Python 3.6)修复了这个问题,因为我可以再次成功运行代码,但根本原因是个谜?

Restarting kernel in Spyder (Python 3.6 on a Dell Desktop) fixes the problem in that I can then run the code again successfully, but the root cause is a mystery?

链接到另一个类似但作为尚未得到答复.

Link to another question that is similar but as yet unanswered.

如果您觉得这个问题值得回答,请为它+1!

Please +1 this question if you find it worthy of an answer!!

import turtle
bob = turtle.Turtle()
print(bob)
for i in range(4):
    bob.fd(100)
    bob.lt(90)

turtle.mainloop()

推荐答案

我意识到这看起来完全不令人满意,但我发现创建乌龟:

I realize this will seem wholly unsatisfactory, but I have found that creating the turtle with:

try:
    tess = turtle.Turtle()
except:
    tess = turtle.Turtle()  

有效(也就是说,消除了每隔一段时间工作"的部分.我也从

works (that is, eliminates the "working every other time" piece. I also start with

wn = turtle.Screen()

并以

from sys import platform
if platform=='win32':
    wn.exitonclick()

如果没有这些部件,如果我尝试在 Windows 中移动海龟图形窗口,事情就会中断.(在 Windows 机器上为 Python 3.6 运行 Spyder)当然,OSX 在没有 exitonclick() 命令的情况下非常满意,并且对它不满意,因此添加了结束功能修复"的平台特定版本.OSX 仍然需要 try...except 部分.

Without those parts, if I try to move the turtle graphics windows in Windows, things break. (running Spyder for Python 3.6 on a Windows machine) edit: of course, OSX is perfectly happy without the exitonclick() command and unhappy with it so added platform specific version of ending "feature fix." The try...except part is still needed for OSX.

这篇关于Python Turtle 窗口每运行 2 次就会崩溃一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆