pyqtgraph实时图在重新启动实时图时中断

时间:2018-09-08 05:41:50

标签: python qthread pyqtgraph

因此,在此程序中,我可以通过将切片器值设置为零来更改切片器值来启动Qthread实时绘图,并停止QThread实时绘图。 但是有时,当我尝试重新启动实时绘图时,该图在短时间内看起来像是坏了,如这张图片所示。

enter image description here

在这张图片中,实际折线图下方有一个单线斜线

这是我的代码:

ui.py

    from pyqtgraph.Qt import QtCore, QtGui
from PyQt4 import QtCore,QtGui,uic
import sys
import time
from collections import deque
from DynamicThread import DynamicThread
from ThreadRun import ThreadRun

from GUI_ui import Ui_home

class ui(QtGui.QTabWidget):
    akselerasi = 0
    curve = None
    dataGraph = deque(maxlen=20)
    x1 = []
    y1 = []
    aa = False

    def __init__(self,parent = None):
        super(ui,self).__init__(parent = parent)

        self.UiMeja = Ui_home()
        self.UiMeja.setupUi(self)

        self.UiMeja.akselerasi.setMinimum(0)
        self.UiMeja.akselerasi.setMaximum(50)
        self.UiMeja.akselerasi.setValue(0)
        self.UiMeja.akselerasi.valueChanged.connect(self.valueAkselerasi)
        self.UiMeja.akselerasi.sliderReleased.connect(self.getThread)

        self.threadDinamis = DynamicThread()
        self.threadDinamis.dataThread.connect(self.runningDynamic)
        ui.curve = self.UiMejaGetar.graph1.plot(pen=(255,0,0))
        self.UiMeja.graph1.setRange(xRange=None, yRange=[-1, 1 ])
        self.UiMeja.graph1.showGrid(x=True, y=True)
        self.UiMeja.graph1.setMouseEnabled(x=False, y=False)
        self.UiMejaGetar.l1.setText(str(self.UiMejaGetar.akselerasi.value()))

    def getThread(self):

        if self.sender() == self.UiMeja.akselerasi:
            ui.akselerasii = int(self.UiMeja.akselerasi.value())

        if GUI_MejaGetar.akselerasii > 0:
            if ui.aa == True:
                print("second")
                minRange = int(self.UiMeja.akselerasi.value()) * -1
                self.UiMeja.graph1.setRange(xRange=None, yRange=[minRange, int(self.UiMeja.akselerasi.value()) ])
                self.threadDinamis.trigrer(ui.akselerasii)

            else:
                minRange = int(self.UiMeja.akselerasi.value()) * -1
                self.UiMeja.graph1.setRange(xRange=None, yRange=[minRange, int(self.UiMeja.akselerasi.value()) ])
                GUI_MejaGetar.aa = True

                print("first")
                self.threadDinamis.trigrer(GUI_MejaGetar.akselerasii)
                self.threadDinamis.start()
        else:
            self.threadDinamis.stop()
            GUI_MejaGetar.aa = False
            GUI_MejaGetar.dataGraph.clear()
            GUI_MejaGetar.x1 = []
            GUI_MejaGetar.y1 = []

    def runningDynamic(self,generateData):
        #print(generateData['x'])
        GUI_MejaGetar.dataGraph.append({'x': generateData['x'], 'y': generateData['y']})
        GUI_MejaGetar.x1 = [item['x'] for item in GUI_MejaGetar.dataGraph]
        GUI_MejaGetar.y1 = [item['y'] for item in GUI_MejaGetar.dataGraph]
        GUI_MejaGetar.curve.setData(x=GUI_MejaGetar.x1, y=GUI_MejaGetar.y1)


if __name__ == '__main__':

    app = QtGui.QApplication([])
    widget = GUI_MejaGetar()

    widget.show()
    app.exec_()

这是我的线程代码:

DynamicThread.py

    import time
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QTime, QTimer
from randomMethod import randomMethod

class DynamicThread(QtCore.QThread):
    dataThread = QtCore.pyqtSignal(object)
    dynamicGenerator = randomMethod()
    status = False
    intAkselerasi = 0
    def __init__(self, parent=None):
        QtCore.QThread.__init__(self,parent)
        #self.gui_text = None

    def trigrer(self,num):
        DynamicThread.intAkselerasi = num

    def do_work(self):
        i = 0
        listData = []
        count = 0
        test = 0
        #t = QTime()
        #t.start()
        print("starting.....")

        while DynamicThread.status == True:
            if DynamicThread.intAkselerasi > 0:
                if not listData:
                    value = int(DynamicThread.dynamicGenerator.bits(), 2)
                    dat = value % DynamicThread.intAkselerasi
                    listData.append(dat)
                    count = dat
                else:
                    value = int(DynamicThread.dynamicGenerator.bits(), 2)
                    dat = value % DynamicThread.intAkselerasi

                    if listData[i - 1] < 0:
                        dat = dat * -1
                    test = 0
                    test = count + dat
                    lowLimit = DynamicThread.intAkselerasi * -1
                    if DynamicThread.intAkselerasi > test > lowLimit:
                        count = count + dat
                        listData.append(count)
                    else:
                        if dat < lowLimit:
                            temp = abs(dat)
                            count = count + temp
                            listData.append(temp)
                        else:
                            temp = dat * -1
                            count = count + temp
                            listData.append(temp)
                print(listData[i])
                print("test: ",test)
                print("count: ",count)
                self.dataThread.emit({'x': i,'y':count})
                i = i + 1
                time.sleep(0.1)
            else:
                DynamicThread.status = False
                print("stop")
                break
        print("stoping the process....")

    def run(self):
        self.do_work()

    def start(self):
        DynamicThread.status = True
        super(DynamicThread, self).start()

    def stop(self):
        DynamicThread.status = False

那么,我的代码有什么问题?知道如何解决吗?谢谢 (nb:我在树莓派3上运行此代码)

1 个答案:

答案 0 :(得分:0)

要解决此问题,我添加

GUI_MejaGetar.dataGraph.clear()
GUI_MejaGetar.x1 = []
GUI_MejaGetar.y1 = []

每次我开始第一时间或结束实时循环