2017-07-16 79 views
0

我正在試圖製作一個程序,其中一個實例將值發送給另一個實例,並不斷繪製它們。我使用pypubsub編程,將一個實例的值發送給另一個實例。另一個實例獲取值並將它們存儲到雙端隊列中,並在每次更新時繪製雙端隊列。如何在使用pyqtgraph和pypubsub獲取值時繪製一個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() 
+0

請簡化您的問題和示例 - 清除所有有用的東西。當基礎數據發生變化時,您正試圖更新圖表。關注你的問題。寫一個簡短的腳本來封裝你的問題 - 用一些僞造的數據;迭代數據並*更新*圖。 [mcve] – wwii

+0

@wwii對不起!由於英語不是我的母語,因此聽起來可能是不連貫的。當它與圖形相關時,解釋問題並不容易。 – maynull

+0

你是從github/schollii/pypubsub使用pubsub嗎? – Schollii

回答

0

看着sendmessage和訂閱,一切看起來不錯。但我注意到你沒有QApplication實例和事件循環。創建應用程序,並在末尾調用exec(),以便它進入事件循環。那麼渲染將會發生。

app = QtGui.QApplication([]) 

plotterObj = Plotter() 
senderObj = Sender() 

pub.subscribe(plotterObj.plot, 'update') 

senderObj.send() 

app.exec() 
相關問題