Reputation: 8068
I have this simple program made with Python 2.7.5
. Basically I'm just drawing bunch of random stuff to the screen, but when I close the canvas I get a weird error.
import turtle
import random
import time
turtle.hideturtle()
class Mus:
def __init__(self):
turtle.setx(random.randint(1,100))
turtle.sety(random.randint(1,100))
turtle.circle(random.randint(1,100))
while True:
Mus()
turtle.exitonclick()
When I close the program, I get this error:
Traceback (most recent call last):
File "/Users/jurehotujec/Desktop/running.py", line 15, in <module>
Mus()
File "/Users/jurehotujec/Desktop/running.py", line 12, in __init__
turtle.circle(random.randint(1,100))
File "<string>", line 1, in circle
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 1908, in circle
self._rotate(w)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 3110, in _rotate
self._update()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 2564, in _update
self._update_data()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 2555, in _update_data
self._pencolor, self._pensize)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 569, in _drawline
self.cv.coords(lineitem, *cl)
File "<string>", line 1, in coords
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2240, in coords
self.tk.call((self._w, 'coords') + args)))
TclError: invalid command name ".4335016920"
What am I doing wrong? I'm new to python so any help would be appreciated :)
Thanks
Upvotes: 2
Views: 1785
Reputation: 4286
Try this
from turtle import *
tim = Turtle()
my_screen = Screen()
#my_screen.exitonclick()
tim.forward(100)
my_screen.exitonclick()
Upvotes: 0
Reputation: 7052
Seems that your loop tries to continue after Tk has destroyed some important objects. You could signal the closing event via a flag:
import turtle
import random
import time
import Tkinter as tk
turtle.hideturtle()
closed = False
def on_close():
global closed
closed = True
exit()
# Register hander for close event
tk._default_root.protocol("WM_DELETE_WINDOW", on_close)
class Mus:
def __init__(self):
turtle.setx(random.randint(1,100))
turtle.sety(random.randint(1,100))
turtle.circle(random.randint(1,100))
# check the flag
while not closed:
Mus()
turtle.exitonclick()
Upvotes: 2