我记得在某处看到过一篇文章,内容是关于如何将python中的绘图从主线程中删除,但我似乎找不到它。我的第一次尝试是这样的,但没有成功。它最初不会崩溃(最终会崩溃),但不会发生绘图。其思想是options
是绘制函数的映射,每个函数都绘制到pyqtgraph或QTWidget等
from threading import *
from Queue import *
anObject1 = DrawingObject()
anObject2 = DrawingObject()
anObject3 = DrawingObject()
options = {
0 : anObject1.drawing_func,
1 : anObject2.drawing_func,
2 : anObject3.drawing_func,
3 : updateNon,
}
def do_work(item): #item is a tuple with the item at 0 is the index to which function
#print str(item) + "\n"
options[item[0]](item)
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
#This function is a callback from C++
def callback(s, tuple):
#options[tuple[0]](tuple) #this works
q.put(tuple) #this does not
num_worker_threads = 3
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
我的理解是,在主GUI线程之外绘制QWidget是不可能的。你可以在Qt论坛和文档中找到很多关于这个的参考资料。但是,可以启动一个子进程,将其绘制到共享内存中的图像中,然后在主进程中显示该图像。这是
pyqtgraph/widgets/RemoteGraphicsView.py
所采用的方法;请参见examples/RemoteSpeedTest.py
以获取此方法的示例。你知道吗相关问题 更多 >
编程相关推荐