Khi tôi gọiRuntimeError: chủ đề chính không phải là trong vòng lặp chính
self.client = ThreadedClient()
trong chương trình Python, tôi nhận được lỗi
"RuntimeError: main thread is not in main loop"
tôi đã làm một số googling, nhưng tôi đang làm cho một lỗi nào đó ... Ai đó có thể giúp tôi không?
Full lỗi:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 530, in __bootstrap_inner
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 483, in run
File "/Users/Wim/Bird Swarm/bird_swarm.py", line 156, in workerGuiThread
self.root.after(200, self.workerGuiThread)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 501, in after
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1098, in _register
RuntimeError: main thread is not in main loop
Lớp học:
class ThreadedClient(object):
def __init__(self):
self.queue = Queue.Queue()
self.gui = GuiPart(self.queue, self.endApplication)
self.root = self.gui.getRoot()
self.running = True
self.GuiThread = threading.Thread(target=self.workerGuiThread)
self.GuiThread.start()
def workerGuiThread(self):
while self.running:
self.root.after(200, self.workerGuiThread)
self.gui.processIncoming()
def endApplication(self):
self.running = False
def tc_TekenVogel(self,vogel):
self.queue.put(vogel)
class GuiPart(object):
def __init__(self, queue, endCommand):
self.queue = queue
self.root = Tkinter.Tk()
Tkinter.Canvas(self.root,width=g_groottescherm,height=g_groottescherm).pack()
Tkinter.Button(self.root, text="Move 1 tick", command=self.doSomething).pack()
self.vogelcords = {} #register of bird and their corresponding coordinates
def getRoot(self):
return self.root
def doSomething():
pass #button action
def processIncoming(self):
while self.queue.qsize():
try:
msg = self.queue.get(0)
try:
vogel = msg
l = vogel.geeflocatie()
if self.vogelcords.has_key(vogel):
cirkel = self.vogelcords[vogel]
self.gcanvas.coords(cirkel,l.geefx()-g_groottevogel,l.geefy()-g_groottevogel,l.geefx()+g_groottevogel,l.geefy()+g_groottevogel)
else:
cirkel = self.gcanvas.create_oval(l.geefx()-g_groottevogel,l.geefy()-g_groottevogel,l.geefx()+g_groottevogel,l.geefy()+g_groottevogel,fill='red',outline='black',width=1)
self.vogelcords[vogel] = cirkel
self.gcanvas.update()
except:
print('Failed, was van het type %' % type(msg))
except Queue.Empty:
pass
Từ traceback của bạn, có vẻ như bạn đang chạy 'workerGuiThread' từ chuỗi bạn đang tạo ở nơi khác, thay vì từ chuỗi chính của quá trình thực hiện. Tôi không phải là chuyên gia TK, nhưng lỗi này dường như gợi ý rằng điều này không được phép (bạn cần phải sử dụng chuỗi chính để gọi các hàm TK, như 'after'). – Blckknght
Xem [câu hỏi này] (http://stackoverflow.com/questions/3567238/i-need-a-little-help-with-python-tkinter-and-threading), [câu trả lời này] (http: // stackoverflow .com/a/10556698/908494), vv để biết thêm chi tiết về việc sử dụng TkInter trong một chương trình đa luồng. Nhưng phiên bản ngắn là: Chỉ sử dụng nó trong chủ đề chính, thời gian. – abarnert
Hey Blckknght. Vì tôi đang sử dụng mtTkinter. – user2040823