简体   繁体   English

Python Turtle 窗口每运行 2 次就会崩溃一次

[英]Python Turtle window crashes every 2nd time running

The code below is a basic square drawing using Turtle in python.下面的代码是在 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.但是再次运行代码会激活一个无响应的 Turtle 窗口,随后每次都会崩溃。

The error message includes raise Terminator and Terminator错误信息包括raise TerminatorTerminator

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

Link to another question that is similar but as yet unanswered. 链接到另一个类似但尚未回答的问题。

Please +1 this question if you find it worthy of an answer!!如果你觉得这个问题值得回答,请 +1 这个问题!!

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()

and end with 并以

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

Without those parts, if I try to move the turtle graphics windows in Windows, things break. 如果没有这些部分,如果我尝试在Windows中移动乌龟图形窗口,事情就会中断。 (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." (在Windows机器上运行用于Python 3.6的Spyder for Python 3.6)编辑:当然,没有exitonclick()命令,OSX非常满意,并且对此不满意,因此添加了特定于平台的版本的“功能修复”。 The try...except part is still needed for OSX. OSX仍然需要try ... except部分。

The module uses a class variable _RUNNING which remains true between executions when running in spyder instead of running it as a self contained script.该模块使用类变量 _RUNNING ,当在 spyder 中运行而不是将其作为自包含脚本运行时,该变量在执行之间保持为真。 I have requested for the module to be updated.我已要求更新该模块。

Meanwhile, work around/working example beyond what DukeEgr93 has proposed同时,解决/工作示例超出了 DukeEgr93 的建议

1) 1)

import importlib
import turtle

importlib.reload(turtle)

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

turtle.mainloop()


import importlib
import turtle

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

turtle.mainloop()


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM