问题
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