I've made a 2D animation of the solar system by using turtle graphics and code from: How to make items draw at the same time in python using turtle? by @cdlane
cdlane's code is:
""" Simulate motion of Mercury, Venus, Earth, and Mars """
planets = {
'mercury': {'diameter': 0.383, 'orbit': 58, 'speed': 7.5, 'color': 'gray'},
'venus': {'diameter': 0.949, 'orbit': 108, 'speed': 3, 'color': 'yellow'},
'earth': {'diameter': 1.0, 'orbit': 150, 'speed': 2, 'color': 'blue'},
'mars': {'diameter': 0.532, 'orbit': 228, 'speed': 1, 'color': 'red'},
}
def setup_planets(planets):
for planet in planets:
dictionary = planets[planet]
turtle = Turtle(shape='circle')
turtle.speed("fastest") # speed controlled elsewhere, disable here
turtle.shapesize(dictionary['diameter'])
turtle.color(dictionary['color'])
turtle.pu()
turtle.sety(-dictionary['orbit'])
turtle.pd()
dictionary['turtle'] = turtle
screen.ontimer(revolve, 50)
def revolve():
for planet in planets:
dictionary = planets[planet]
dictionary['turtle'].circle(dictionary['orbit'], dictionary['speed'])
screen.ontimer(revolve, 50)
screen = Screen()
setup_planets(planets)
screen.exitonclick()
Its important to note that this code works fine on the first run, however, when run more than once, I receive this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\lach3\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\lach3\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 814, in callit
func(*args)
File "c:\Users\lach3\Desktop\Prog\from turtle import Turtle, Screen.py", line 31, in revolve
dictionary['turtle'].circle(dictionary['orbit'], dictionary['speed'])
File "C:\Users\lach3\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 1998, in circle
self.speed(speed)
File "C:\Users\lach3\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2175, in speed
self.pen(speed=speed)
File "C:\Users\lach3\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2460, in pen
self._update()
File "C:\Users\lach3\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2661, in _update
self._update_data()
File "C:\Users\lach3\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2647, in _update_data
self.screen._incrementudc()
File "C:\Users\lach3\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 1293, in _incrementudc
raise Terminator
turtle.Terminator
I've tried getting rid of screen.exitonclick() all together and replacing it with alternative exiting methods such as exiting with a key press or just having the animation close when the window is closed. Every method receives the same error...
How can I fix this?