2011-05-01 5 views
1

세계! 파이썬의 거북이 그래픽에서는 다양한 거북 개체를 만들고 그 방법으로 앞으로, 뒤로 이동할 수 있습니다 ... 스레드를 실험하고 싶었 기 때문에 MyTurtleManipulator라는 스레드 클래스를 작성했습니다. 실험으로 Tkinter 거북이 및 스레드

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 

, 스레드가 주 루프에 없다 "는 의미입니다. 도와 줘서 고마워.

답변

1

이, 거북이와 스레드가 친구 아닌 파이썬/Tkinter를 제한 as described here 것으로 보인다

+0

아이 당신의 도움에 대한 슬픈 ... 감사의 – rfrm

0

이 한계를 가지고 있지만 여전히 가능하다. 대답은 오류 메시지입니다 :

RuntimeError: main thread is not in main loop

거북이/tkinter 작업을 주 스레드로 제한하십시오. 아래 예 내 재 작업, 나는 스레드와 거북이 사이의 통신에 스레드 안전 데이터 구조를 사용했습니다 : 난 나는 단지와 기계에 1로 QUEUE_SIZE을 설정

from threading import Thread, active_count 
from turtle import Turtle, Screen 
import queue 
import random 

QUEUE_SIZE = 1 # set higher the more hardware threads you have 
ACTIONS = [Turtle.forward, Turtle.right, Turtle.left] 
COLORS = ['red', 'black', 'blue', 'green', 'magenta'] 

class MyTurtleManipulator(Thread): 

    def __init__(self, turtle): 
     super().__init__() 
     self.turtle = turtle 

    def run(self): 
     for _ in range(100): 
      actions.put((self.turtle, random.choice(ACTIONS), random.randint(1, 30))) 

def process_queue(): 
    while not actions.empty(): 
     turtle, action, argument = actions.get() 
     action(turtle, argument) 

    if active_count() > 1: 
     screen.ontimer(process_queue, 100) 

actions = queue.Queue(QUEUE_SIZE) 

for color in COLORS: 
    turtle = Turtle('turtle') 
    turtle.color(color) 
    turtle.setheading(random.randint(0, 360)) 
    MyTurtleManipulator(turtle).start() 

screen = Screen() 

process_queue() 

screen.mainloop() 

두 개의 스레드! 나는 모든 것이 더 많은 스레드로 시스템에서 제대로 작동하고 있는지 알고 궁금 해요 QUEUE_SIZE ~ = #threads - 1

enter image description here