问题
I have tried to make functions for turtle to make it extremely easy to draw shapes and the code looks like this
import turtle as t
def square():
tw = t.Screen()
for i in range(4):
t.forward(100)
t.right(90)
tw.exitonclick()
def triangle():
tw = t.Screen()
for i in range(3):
t.forward(100)
t.right(120)
tw.exitonclick()
def star():
tw = t.Screen()
for i in range(5):
t.forward(150)
t.right(144)
tw.exitonclick()
when i run this code in shell a problem is fonud...
>>> square()
>>> triangle()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
triangle()
File "C:\Users\Manop\Desktop\XENON\turtleg.py", line 11, in triangle
t.forward(100)
File "<string>", line 5, in forward
turtle.Terminator
>>> star()
>>> square()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
square()
File "C:\Users\Manop\Desktop\XENON\turtleg.py", line 5, in square
t.forward(100)
File "<string>", line 5, in forward
turtle.Terminator
>>>
can't get over what the problem is, because i even used exitonclick()
回答1:
I had the same error while I was working on a school project.
After some research on the turtle library I have found a variable called TurtleScreen._RUNNING
, if this variable is set to True
a turtle window opens, if not you get the turtle.Terminator
error.
Every time you close a turtle screen, TurtleScreen._RUNNING
is automatically set to True
, if you want to avoid that, you can simply write this line of code TurtleScreen._RUNNING = True
(of course you need to import turtle before).
回答2:
Your turtle program is structured incorrectly. You needn't do:
tw = t.Screen()
...
tw.exitonclick()
in every function. Screen()
only needs to be called once; exitonclick()
should only ever be called once. Try this restructuring:
import turtle as t
def square():
for i in range(4):
t.forward(100)
t.right(90)
def triangle():
for i in range(3):
t.forward(100)
t.right(120)
def star():
for i in range(5):
t.forward(150)
t.right(144)
t.penup()
t.goto(150, 150)
t.pendown()
square()
t.penup()
t.goto(-150, 150)
t.pendown()
triangle()
t.penup()
t.goto(150, -150)
t.pendown()
star()
screen = t.Screen()
screen.exitonclick()
If you want to execute the code interactively, that's fine too. Just drop everything after the function definitions, load it into Python interactively and do:
>>> star()
or whatever you want to run. You don't need the call to Screen()
and the exitonclick()
doesn't make sense when working interactively.
回答3:
Let the method screen.exitonclick() be the last statement in your code without indenting it.
You use this method when your using a Python IDE such as Pycharm, Spyder etc.
I don't know if you have heard of the method screen.mainloop()
This method enables you see the output of your code when you run it in a Python IDE.
Without this method, your output would appear in a flash.
I rewrote your code and here's mine
from turtle import Turtle
t=Turtle()
def square():
t.up()
t.setpos(-50,-50)
t.down()
for i in range(4):
t.forward(100)
t.right(90)
def triangle():
t.up()
t.setpos(50,50)
t.down()
for i in range(3):
t.forward(100)
t.right(120)
def star():
t.up()
t.setpos(-200,100)
t.down()
for i in range(5):
t.forward(150)
t.right(144)
square()
triangle()
star()
t.screen.exitonclick()
Here's the output output of my program
You can also check this excellent guide in Python turtle
回答4:
When you interrupt the turtle drawing, it gets angry and produces "abnormal termination" error. Use a "running" flag to stop the process at any point:
from turtle import Turtle
t=Turtle()
def square():
global running
t.up()
t.setpos(-50,-50)
t.down()
for i in range(4):
if not running: break; # Check 'running' here
t.forward(100)
t.right(90)
def triangle():
global running
t.up()
t.setpos(50,50)
t.down()
for i in range(3):
if not running: break; # Check 'running' here
t.forward(100)
t.right(120)
def star():
global running
t.up()
t.setpos(-200,100)
t.down()
for i in range(5):
if not running: break; # Check 'running' here
t.forward(150)
t.right(144)
def stop(x,y): # x,y are dummy but they are requested
global running
running = False # Disable running
t.screen.onclick(stop) # Set a function for 'running'
running = True # Enable running
square()
triangle()
star()
I tested the above code. Termination was smooth at all times.
来源:https://stackoverflow.com/questions/45534458/python-turtle-terminator-even-after-using-exitonclick