我打电话的时候
self.client = ThreadedClient()
Run Code Online (Sandbox Code Playgroud)
在我的Python程序中,我收到错误
"RuntimeError:主线程不在主循环中"
我已经做了一些谷歌搜索,但我在某种程度上犯了错误...有人可以帮助我吗?
完整错误:
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
Run Code Online (Sandbox Code Playgroud)
类别:
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) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 python 下并行运行多个重复性任务。我对多处理很陌生,但由于所有任务都是独立的,我使用了以下简单的代码:
import numpy as np
import sys
import os
import glob
import matplotlib.pyplot as plt
import concurrent.futures as Cfut
def analize(simul, N_thread):
path = os.getcwd()
print('Analizing topo ...')
Data = output of some calculations
print('Writing Data ...')
np.save('Data', Data)
print('Data saved')
print('Printing figures')
plt.figure()
plt.plot(Data[0])
plt.savefig('figure1.pdf)
plt.clf()
plt.plot(Data[0])
plt.savefig('figure1.pdf)
plt.close('all')
print('figures saved')
os.chdir(path
if __name__ == '__main__':
list_simul = sorted(glob.glob('Layer*'))
executor = Cfut.ProcessPoolExecutor(5)
#executor = Cfut.ThreadPoolExecutor(N_jobs)
futures = [executor.submit(analize, item, 10) for item in list_simul]
Cfut.wait(futures)
Run Code Online (Sandbox Code Playgroud)
它在 3 …
我编写了一个可以传递图像的简短模块,只需创建一个Tkinter窗口并显示它.我遇到的问题是,即使我实例化并调用在单独的线程中显示图像的方法,主程序也不会继续,直到Tkinter窗口关闭.
这是我的模块:
import Image, ImageTk
import Tkinter
class Viewer(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
def show(self,img):
self.to_display = ImageTk.PhotoImage(img)
self.label_image = Tkinter.Label(self,image=self.to_display)
self.label_image.grid(column = 0, row = 0, sticky = "NSEW")
self.mainloop()
Run Code Online (Sandbox Code Playgroud)
它似乎工作正常,除非我从我的测试程序中调用它,如下所示,它似乎不允许我的测试程序继续,即使在另一个线程中启动.
import Image
from viewer import Viewer
import threading
def showimage(im):
view = Viewer(None)
view.show(im)
if __name__ == "__main__":
im = Image.open("gaben.jpg")
t = threading.Thread(showimage(im))
t.start()
print "Program keeps going..."
Run Code Online (Sandbox Code Playgroud)
我想也许我的问题是我应该在模块本身内创建一个新线程,但我只想尝试保持简单,因为我是Python的新手.
无论如何,提前感谢任何帮助.
编辑:为了清楚起见,我只是想制作一个在Tkinter窗口中显示图像的模块,以便我可以在任何时候想要显示图像时使用该模块.我遇到的问题是,只要程序使用此模块,它就无法恢复,直到Tkinter窗口关闭.