ich bin schon den ganzen Tag am suchen. Ich habe ein kleines Programm (Turtlegraphic) welches drei Schildkröten durch ein Fenster rennen lässt.
Die drei bewegen sich auch gleichzeitig alles so weit gut.
Aber das beenden der Threads will mir nicht gelingen. Wenn ich mit der Maus in das Fenster klicke dann beendet sich das Programm mit einer Fehlermeldung.

Hier das Skript
Code: Alles auswählen
#!/usr/bin/env python
#coding: utf8
from threading import Thread
from turtle import *
import turtle
import random
class Turtanimator(Thread):
def __init__(self, turtle):
Thread.__init__(self)
self.t = turtle
def run(self):
self.t.shape("turtle")
while True:
self.t.forward(150 * random.random())
self.t.left(-180 + 360 * random.random())
# Schildkröte a definieren
a = Turtle()
a.up()
a.setpos(0,0)
a.color("red")
a.down()
a.speed(10)
# Schildkröte b definieren
b = Turtle()
b.up()
b.setpos(0,0)
b.color("blue")
b.down()
b.speed(10)
# Schildkröte c definieren
c = Turtle()
c.up()
c.setpos(0,0)
c.color("green")
c.down()
c.speed(10)
thread1 = Turtanimator(a)
thread2 = Turtanimator(b)
thread3 = Turtanimator(c)
thread1.start()
thread2.start()
thread3.start()
turtle.exitonclick()
Fehlermeldung
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "./thread3.py", line 19, in run
self.t.left(-180 + 360 * random.random())
File "/usr/lib/python2.7/lib-tk/turtle.py", line 1614, in left
self._rotate(angle)
File "/usr/lib/python2.7/lib-tk/turtle.py", line 3110, in _rotate
self._update()
File "/usr/lib/python2.7/lib-tk/turtle.py", line 2565, in _update
self._drawturtle()
File "/usr/lib/python2.7/lib-tk/turtle.py", line 2825, in _drawturtle
width=w, top=True)
File "/usr/lib/python2.7/lib-tk/turtle.py", line 540, in _drawpoly
self.cv.itemconfigure(polyitem, fill=fill)
File "<string>", line 1, in itemconfigure
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2412, in itemconfigure
return self._configure(('itemconfigure', tagOrId), cnf, kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1325, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: invalid command name ".140180658307944"
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "./thread3.py", line 19, in run
self.t.left(-180 + 360 * random.random())
File "/usr/lib/python2.7/lib-tk/turtle.py", line 1614, in left
self._rotate(angle)
File "/usr/lib/python2.7/lib-tk/turtle.py", line 3110, in _rotate
self._update()
File "/usr/lib/python2.7/lib-tk/turtle.py", line 2564, in _update
self._update_data()
File "/usr/lib/python2.7/lib-tk/turtle.py", line 2550, in _update_data
self.screen._incrementudc()
File "/usr/lib/python2.7/lib-tk/turtle.py", line 1239, in _incrementudc
raise Terminator
Terminator
Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "./thread3.py", line 19, in run
self.t.left(-180 + 360 * random.random())
File "/usr/lib/python2.7/lib-tk/turtle.py", line 1614, in left
self._rotate(angle)
File "/usr/lib/python2.7/lib-tk/turtle.py", line 3108, in _rotate
self._update()
File "/usr/lib/python2.7/lib-tk/turtle.py", line 2567, in _update
screen._delay(screen._delayvalue) # TurtleScreenBase
File "/usr/lib/python2.7/lib-tk/turtle.py", line 590, in _delay
self.cv.after(delay)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 591, in after
self.tk.call('after', ms)
RuntimeError: main thread is not in main loop