Python Turtle.terminator Even After Using Exitonclick()
Solution 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).
Solution 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.
Solution 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 inrange(4):
t.forward(100)
t.right(90)
def triangle():
t.up()
t.setpos(50,50)
t.down()
for i inrange(3):
t.forward(100)
t.right(120)
def star():
t.up()
t.setpos(-200,100)
t.down()
for i inrange(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
Solution 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()
defsquare():
global running
t.up()
t.setpos(-50,-50)
t.down()
for i inrange(4):
ifnot running: break; # Check 'running' here
t.forward(100)
t.right(90)
deftriangle():
global running
t.up()
t.setpos(50,50)
t.down()
for i inrange(3):
ifnot running: break; # Check 'running' here
t.forward(100)
t.right(120)
defstar():
global running
t.up()
t.setpos(-200,100)
t.down()
for i inrange(5):
ifnot running: break; # Check 'running' here
t.forward(150)
t.right(144)
defstop(x,y): # x,y are dummy but they are requestedglobal 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.
Solution 5:
I've had this same error... add white space between forward and the parenthesis. Solved it for me.
Post a Comment for "Python Turtle.terminator Even After Using Exitonclick()"