Python Turtle window crashes every 2nd time running

两盒软妹~` 提交于 2021-02-08 07:51:46

问题


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

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.

The error message includes raise Terminator and Terminator

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.

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

回答1:


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



来源:https://stackoverflow.com/questions/50438762/python-turtle-window-crashes-every-2nd-time-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!