我正在尝试创建一个程序,其中一个实例将值发送到另一个实例,并且它会连续绘制它们。我使用pypubsub对其进行编程,以便将值从一个实例发送到另一个实例。另一个实例获取值并将它们存储到双端队列中,并在更新时绘制双端队列。
我认为这些实例很好地相互通信,我可以看到deque每秒都按照我的计划更新,但是,问题是图表在更新后不显示deque的值,而是显示值一次整个更新已经完成。我想知道如何在更新时绘制双端队列。
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
from collections import deque
from pubsub import pub
import time
class Plotter:
def __init__(self):
self.deq = deque()
self.pw = pg.GraphicsView()
self.pw.show()
self.mainLayout = pg.GraphicsLayout()
self.pw.setCentralItem(self.mainLayout)
self.p1 = pg.PlotItem()
self.p1.setClipToView=True
self.curve_1 = self.p1.plot(pen=None, symbol='o', symbolPen=None, symbolSize=10, symbolBrush=(102, 000, 000, 255))
self.mainLayout.addItem(self.p1, row = 0, col=0, rowspan=2)
def plot(self, msg):
print('Plotter received: ', msg)
self.deq.append(msg)
print(self.deq)
self.curve_1.setData(self.deq)
class Sender:
def __init__(self):
self.list01 = [1,2,3,4,5] # A list of values that will be sent through pub.sendMessage
def send(self):
for i in range(len(self.list01)):
pub.sendMessage('update', msg = self.list01[i] )
time.sleep(1)
plotterObj = Plotter()
senderObj = Sender()
pub.subscribe(plotterObj.plot, 'update')
senderObj.send()
看看sendmessage和订阅,一切看起来都不错。但我注意到你没有QApplication实例和事件循环。创建应用程序,并在最后调用exec(),以便进入事件循环。然后将进行渲染。
app = QtGui.QApplication([])
plotterObj = Plotter()
senderObj = Sender()
pub.subscribe(plotterObj.plot, 'update')
senderObj.send()
app.exec()