在绘制实时数据时,我很难使gui响应。为了使GUI不会冻结,我尝试对所有 Activity 进行线程化。我想了解以下内容:
1和2已完成,但是现在我不确定如何在单独的线程中更新绘图。
我的PlotWidget启动看起来像这样:
self.plottingDQ = [queue.deque(maxlen=100), queue.deque(maxlen=100), queue.deque(maxlen=100)]
self.graph = pg.PlotWidget()
self.barItem = pg.BarGraphItem(x0=self.plottingDQ[0], y0=self.plottingDQ[1], width=self.plottingDQ[2], height=1)
self.graph.addItem(self.barItem)
通过连接到该功能的按钮完成启动线程。 Writer-Thread是无关紧要的,因为它与绘图无关。但是计算器线程会计算数据以更新绘图def startPlotting(self):
# not relevant for the example
self.csvData = queue.Queue()
self.csv = Writer(self.csvData)
self.csv.setDaemon(True)
self.csv.start()
self.calcData = queue.Queue()
self.calcPlot = Calculator(self.calcData, self.plottingDQ)
self.calcPlot.setDaemon(True)
self.calcPlot.start()
# Timer to update plot every x ms
self.timer = QTimer()
self.timer.timeout.connect(self.updatePlot)
self.timer.start(500)
现在我每500毫秒更新一次Qtimer中的绘图def updatePlot(self):
print("update")
self.barItem.setOpts()
因此,每当我从串行端口获得一些输入时,我都将数据传递给线程并调用如下代码:def fromPort(self, data):
self.csvData.put(data)
self.calcData.put(data)
在我的计算器线程中,将计算数据并将其移交给与BarGraphItem连接的plottingDQclass Calculator(threading.Thread):
def __init__(self, calcData, plottingDQ):
threading.Thread.__init__(self)
self.calcData = calcData
self.plottingDQ = plottingDQ
self.a = threading.Event()
self.a.set()
def run(self):
while self.a.isSet():
# Do some stuff here ...
# After the calculations, i write the data into the plottingDQ
self.plottingDQ[0].append(x)
self.plottingDQ[1].append(y)
self.plottingDQ[2].append(duration)
这是将我的计算数据从Calcualtor-Thread传递到BarGraphItem中使用的双端队列的正确方法吗?如何在线程中更新BarGraphItem?
最佳答案
您编程的方式看起来不错。
“拖停”的根本原因似乎是拖动过程中的“更新块”。
尝试通过将pg.QtGui.QApplication.processEvents()添加到updatePlot函数(described here)来强制执行更新:
def updatePlot(self):
print("update")
self.barItem.setOpts()
pg.QtGui.QApplication.processEvents()
关于python - 更新线程内的pyqtgraph BarGraphItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65183612/