【问题标题】:Getting the drawing off the main thread让绘图脱离主线程
【发布时间】:2015-07-02 09:52:42
【问题描述】:

我记得在某处看到一篇关于能够从主线程中获取 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()

【问题讨论】:

标签: python qt python-multithreading pyqtgraph


【解决方案1】:

我的理解是不可能在主 GUI 线程之外绘制到 QWidget。您可以在 Qt 论坛和文档中找到许多对此的参考。但是,可以启动一个子进程,将图像绘制到共享内存中,然后在主进程中显示该图像。这是pyqtgraph/widgets/RemoteGraphicsView.py采取的方法;有关此示例,请参阅 examples/RemoteSpeedTest.py

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 2019-02-27
    • 2018-10-27
    • 1970-01-01
    • 2021-04-22
    相关资源
    最近更新 更多