pyqtgraph plot widget不更新

时间:2016-11-25 10:01:57

标签: python widget pyqt5 pyqtgraph

我有两个工作流 1 - 从com端口接收数据并发送信号。

def __packet_parser(self, *args):
        while self.__thred_is_runing:
            data = self.__connection.read_all()
            if data != b'':
                self.change_data.emit(self.__readline())
                self.__callback(self.__readline()) 

2 - 绘制图表。

def set_value_by_plot_name(self, value, name='default'):
          self.__plots[name].setData(np.append(self.__plots[name].getData()[1][1:], value))  
def __test(self, value):
        tmp = value.decode('windows-1252')
        data = tmp.split('\t')
        if len(data) == 10:
            self.__gr.set_value_by_plot_name(int(data[1]))
def main(self):
        self.__window = Window()
        self.__gr = Graphics()
        w = self.__gr.get_widget()
        self.__window.add_widget(w)
        connect = COMPortConnection('COM7', 38400)
        connect.change_data.connect(self.__test)
        connect.open()
        self.__window.show()

几秒钟一切正常,然后不再更新。 什么问题? 数据更新,图片不是。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,我通过中间方法解决了这个问题,我不知道为什么:

class GraphWindowController:

   # init ...

    def start_graph(self):
        # ... process
        self.view.run_graph(self.graph_nodes)
        stream_signals_and_slots = []
        my_steam_signal.connect(self.add_value_to_graph)

    def add_value_to_graph(self, values):
        self.view.update(values)




class GraphWindow(QtGui.QFrame, Ui_GraphWindow):

    def __init__(self, parent=None):
       """Initializer."""
       super(GraphWindow, self).__init__(parent)
       self.setupUi(self)
       self.connect_signals()
       self.plot_widget = None
       self.y_data = []
       self.x_data = []
       self.curve = []
       self.display_range = None
       self.velocity = None
       self.values_to_display = None
       self.count_value_to_display = 0
       self.legend = None
       self.build_gui()

   # Doing stuff ...

   def build_gui(self):
       self.plot_widget = pg.PlotWidget()
       layout = QtGui.QGridLayout()
       self.PlotLayout.setLayout(layout)
       layout.addWidget(self.plot_widget, 0,1)

   def configure_graph(self, display_range, velocity):

       self.display_range = display_range
       self.velocity = velocity

       self.plot_widget.setLabels(left='axis 1')
       self.legend = pg.LegendItem((100, 60), offset=(70, 30))
       self.legend.setParentItem(self.plot_widget.graphicsItem())
       self.plot_widget.showGrid(True, True, 251)
       self.plot_widget.showButtons()

       self.plot_widget.setRange(rect=None, xRange=[0, self.display_range], yRange=[
                       1300, -300], padding=0.09, update=True, disableAutoRange=True)

   def run_graph(self, values_to_display):

       self.values_to_display = values_to_display
       self.curve_number = len(self.values_to_display)
       for i in range(self.curve_number):
        self.y_data.append([])
       colors_to_use = self.enumerate_colors(
        self.curve_number)
    # Blablabla doing stuff ...

   def update(self, my_values):
       value = my_values
       for i in range(self.curve_number):
          if len(self.y_data[i]) >= int((self.display_range * 1000) / self.velocity):
               del self.y_data[i][0]
           else:
               my_var = len(self.x_data) * (self.velocity / 1000.0)
               self.x_data.append(my_var)
           self.y_data[i].append(int(value[i]))
           my_data_to_display = list(
               zip(self.x_data, self.y_data[i]))
           my_array = np.array(my_data_to_display)
           self.curve[i].setData(my_array)