【发布时间】:2017-10-02 19:49:53
【问题描述】:
我一直在为 beagle bone black 开发一个 GUI,当单击按钮并开始通过 SPI 获取数据时,它会启动一个线程。
此函数位于名为 Scanner(QObject) 的类中,并在单击“开始”按钮时在不同的线程中运行。
def scan (self):
thread_name = QThread.currentThread().objectName()
self.sig_msg.emit('Scanning '+thread_name)
for step in range(nsamples):
data = self.read_reg(reg[thread_name])
self.sig_data.emit(step, data)
QThread.currentThread().msleep(50)
app.processEvents()
if self.__abort:
self.sig_msg.emit('scan stopped by user')
break
self.sig_done.emit(thread_name)
sig_msg 是一个 pyqtsignal 连接到 GUI 线程内的以下函数。
@pyqtSlot(int, int)
def on_scaner_data(self, t: int, y: int):
app.processEvents()
self.debugBox.insertPlainText('t: '+str(t)+'y: '+str(y)+'\n')
self.debugBox.ensureCursorVisible()
self.MainGraph.update_fig(t,y)
最后调用 MainGraph.update_fig()。在该函数中,我使用了 setData(self.datat,self.datay) 和 app.processEvents() 来更新图形,但没有任何变化。如果我运行 plot(self.datat,self.datay) ,它会重绘图形,但会导致巨大的性能损失。
class DynamicPlotter(PlotWidget):
def __init__(self,parent=None):
PlotWidget.__init__(self)
self.setParent(parent)
# Use getPlotItem() to get the PlotItem inside PlotWidget.
self.pitem = self.getPlotItem()
#now pitem is our PlotItem
self.pitem.curve=self.pitem.plot()
#curve is a new PlotDataItem added by PlotItem.plot()
self.datat = [1,2]
self.datay = [1,2]
self.pitem.curve.setData(self.datat,self.datay)
#this graph works fine
self.datat = []
self.datay = []
def update_fig(self,t:int,y:int):
self.datat.append(t)
self.datay.append(y)
#it works
self.pitem.curve=self.pitem.plot(self.datat,self.datay)
#it doesn't
self.pitem.curve.setData(self.datat,self.datay)
app.processEvents()
print (self.datat)
log.debug(str(t)+str(y))
def reset_figure(self):
log.debug('clean graph')
self.clear()
我一直在关注 pyqtplot 中的这个例子,我的想法是在我的 GUI 中做类似的事情。
import initExample
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time
app = QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('pyqtgraph example: PlotSpeedTest')
p.setRange(QtCore.QRectF(0, -10, 5000, 20))
p.setLabel('bottom', 'Index', units='B')
curve = p.plot()
data = np.random.normal(size=(50,5000))
ptr = 0
lastTime = time()
fps = None
def update():
global curve, data, ptr, p, lastTime, fps
curve.setData(data[ptr%10])
ptr += 1
now = time()
dt = now - lastTime
lastTime = now
if fps is None:
fps = 1.0/dt
else:
s = np.clip(dt*3., 0, 1)
fps = fps * (1-s) + (1.0/dt) * s
p.setTitle('%0.2f fps' % fps)
app.processEvents() ## force complete redraw
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
我一直在阅读文档并且知道我不确定问题出在哪里。我打赌线程或事件循环处理程序,但我不知道。 哪些是我必须审查的关键点? 有什么线索吗?
谢谢。
【问题讨论】:
-
我建议您在帖子中添加
pyqtgraph标签。我会自己做,但您必须删除另一个标签(最多可以有 5 个标签)。
标签: python multithreading pyqt5 beagleboneblack pyqtgraph