мир! В графике черепах в Python есть возможность создавать различные объекты Turtle и манипулировать ими с помощью их методов, вперед, назад ... Я хотел поэкспериментировать с потоками, поэтому я написал многопоточный класс под названием MyTurtleManipulator.
from threading import Thread
from cTurtle import Turtle
import random
class MyTurtleManipulator(Thread):
def __init__(self, turtle):
Thread.__init__(self)
self.turtle=turtle
def run(self):
actions=[Turtle.forward, Turtle.right, Turtle.left]
while True:
action=random.choice(actions)
action(self.turtle, random.randint(1,25))
turtles=[Turtle() for i in range(5)]
threads=[MyTurtleManipulator(turtle) for turtle in turtles]
for thread in threads:
print(thread)
thread.start()
В ходе эксперимента я ожидал увидеть всех черепах, движущихся "одновременно" и случайно, но когда я запускаю программу, я получаю следующие ошибки:
<MyTurtleManipulator(Thread-1, initial)>
<MyTurtleManipulator(Thread-2, initial)>
<MyTurtleManipulator(Thread-3, initial)>
<MyTurtleManipulator(Thread-4, initial)>
<MyTurtleManipulator(Thread-5, initial)>
>>> Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.1/threading.py", line 516, in _bootstrap_inner
self.run()
File "/home/rfrm/test.py", line 13, in run
action(self.turtle, random.randint(1,25))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 1162, in forward
checkargs((int, float))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 1131, in _go
ende = self._position + self._orient * distance
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 2266, in _goto
(start, self._position),
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 419, in _drawline
cl.append(-y)
File "<string>", line 1, in coords
File "/usr/lib/python3.1/tkinter/__init__.py", line 2123, in coords
self.tk.call((self._w, 'coords') + args)))
RuntimeError: main thread is not in main loop
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python3.1/threading.py", line 516, in _bootstrap_inner
self.run()
File "/home/rfrm/test.py", line 13, in run
action(self.turtle, random.randint(1,25))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 1203, in right
checkargs((int, float))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 2300, in _rotate
self._orient = self._orient.rotate(delta)
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 2085, in _update
for t in screen._turtles:
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 2219, in _drawturtle
screen._drawpoly(titem, shape, fill=fc, outline=oc,
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 384, in _drawpoly
cl.append(-y)
File "<string>", line 1, in coords
File "/usr/lib/python3.1/tkinter/__init__.py", line 2123, in coords
self.tk.call((self._w, 'coords') + args)))
RuntimeError: main thread is not in main loop
Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/lib/python3.1/threading.py", line 516, in _bootstrap_inner
self.run()
File "/home/rfrm/test.py", line 13, in run
action(self.turtle, random.randint(1,25))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 1162, in forward
checkargs((int, float))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 1131, in _go
ende = self._position + self._orient * distance
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 2270, in _goto
screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 419, in _drawline
cl.append(-y)
File "<string>", line 1, in coords
File "/usr/lib/python3.1/tkinter/__init__.py", line 2123, in coords
self.tk.call((self._w, 'coords') + args)))
RuntimeError: main thread is not in main loop
Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/lib/python3.1/threading.py", line 516, in _bootstrap_inner
self.run()
File "/home/rfrm/test.py", line 13, in run
action(self.turtle, random.randint(1,25))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 1162, in forward
checkargs((int, float))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 1131, in _go
ende = self._position + self._orient * distance
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 2270, in _goto
screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 419, in _drawline
cl.append(-y)
File "<string>", line 1, in coords
File "/usr/lib/python3.1/tkinter/__init__.py", line 2123, in coords
self.tk.call((self._w, 'coords') + args)))
RuntimeError: main thread is not in main loop
Exception in thread Thread-5:
Traceback (most recent call last):
File "/usr/lib/python3.1/threading.py", line 516, in _bootstrap_inner
self.run()
File "/home/rfrm/test.py", line 13, in run
action(self.turtle, random.randint(1,25))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 1203, in right
checkargs((int, float))
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 2300, in _rotate
self._orient = self._orient.rotate(delta)
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 2085, in _update
for t in screen._turtles:
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 2219, in _drawturtle
screen._drawpoly(titem, shape, fill=fc, outline=oc,
File "/usr/local/lib/python3.1/dist-packages/cTurtle.py", line 384, in _drawpoly
cl.append(-y)
File "<string>", line 1, in coords
File "/usr/lib/python3.1/tkinter/__init__.py", line 2123, in coords
self.tk.call((self._w, 'coords') + args)))
RuntimeError: main thread is not in main loop
Что это значит, что означает "основной поток не в основном цикле". Спасибо за вашу помощь.