real time graph is freezing in raspberry pi

132 views
Skip to first unread message

Jan Sebastian

unread,
May 15, 2018, 11:55:52 AM5/15/18
to pyqtgraph

I write an application that displays random number in real time graph, it run perfectly on windows but when I try it on raspberry pi it alwarys stop updating the graph, but the proses in the python shell is still running generate a rundom number and the program didn't.

Any idea, how to fix it?

the example of my code:


from PyQt4 import QtCore,QtGui,uic
from PyQt4.QtCore import QTime, QTimer
import sys
import time
import threading
from pyqtgraph import ViewBox, PlotWidget
#import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
from blupblupshub import BlumBlumShub
from TimeAxisItem import TimeAxisItem
from collections import deque
import random


qtCreatorFile = "mejaGetar2.ui"


Ui_MainWindow,QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QtGui.QTabWidget,Ui_MainWindow):
    akselerasi = 0
    threads = []
    threads2 = []
    varGraph1 = object 
    aa = False
    def __init__(self):

        QtGui.QTabWidget.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.akselerasi.setMinimum(0)
        self.akselerasi.setMaximum(30)
        self.akselerasi.setValue(0)
        
        self.akselerasi.sliderReleased.connect(self.getThread)
        self.graph1.setRange(xRange=None, yRange=[-1, 1 ])
        self.graph1.setLabel('bottom', 'time', 's')
        self.graph1.showGrid(x=True, y=True)
        #self.graph1.setLab('bottom',TimeAxisItem(orientation='bottom'))
        self.graph1.setMouseEnabled(x=False, y=False)
        MyApp.varGraph1 = self.graph1

        self.l1.setText(str(self.akselerasi.value()))
        #self.currentChanged.connect(self.forceCloseThread)

        
    def getThread(self):
        
        if self.sender() == self.akselerasi:
            MyApp.akselerasii = int(self.akselerasi.value())

        if MyApp.akselerasii > 0:
            
            thread1 = DynamicThread("akselerasi")
            
            if MyApp.aa == True:
                print("second")
                minRange = int(self.akselerasi.value()) * -1
                self.graph1.setRange(xRange=None, yRange=[minRange, int(self.akselerasi.value()) ])
                thread1.trigrer(MyApp.akselerasii)
                
            else:
                thread1.trigrer(MyApp.akselerasii)
                MyApp.threads2.append(thread1)
                minRange = int(self.akselerasi.value()) * -1
                self.graph1.setRange(xRange=None, yRange=[minRange, int(self.akselerasi.value()) ])
                MyApp.aa = True
                
                print("first")
                thread1.start()
        else:
            for t in MyApp.threads2:
                MyApp.aa = False
                t.stop()
                self.graph1.clear()

        
class DynamicThread(threading.Thread,MyApp):
    intAkselerasi = 0
    ruuun = False

    def __init__(self,sub):
        threading.Thread.__init__(self)
        
        self.subject = sub
        
    def trigrer(self,num):
        DynamicThread.intAkselerasi = num
        
    def run(self):
        listData = []
        a = 0
        count = 0
        test = 0
        t = QTime()
        t.start()
        dataGraph = deque(maxlen=20)
        x1 = []
        y1 = []
        
        print("starting " + self.subject)
        dynamicGenerator = BlumBlumShub()
        curve = MyApp.varGraph1.plot(pen=(255,0,0))
        while DynamicThread.ruuun == True:
            if DynamicThread.intAkselerasi > 0:
                minus = DynamicThread.intAkselerasi *-1
                data = random.randint(minus,DynamicThread.intAkselerasi)
                
                dataGraph.append({'x': t.elapsed(), 'y': data})
                x1 = [item['x'] for item in dataGraph]
                y1 = [item['y'] for item in dataGraph]
                curve.setData(x=x1, y=y1)
                print("test: ",data)
                print("\n")
                a = a + 1

                time.sleep(0.5)
                
            else:
                DynamicThread.ruuun = False
                print("stop")
                break
        print("stoping " + self.subject)
        dataGraph = []
        x1 = []
        y1 = []
        
    def start(self):
        DynamicThread.ruuun = True
        super(DynamicThread,self).start()

    def stop(self):
        DynamicThread.ruuun = False

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())



Luke Campagnola

unread,
May 15, 2018, 12:17:41 PM5/15/18
to pyqt...@googlegroups.com
My first thought is that the example is attempting to perform GUI operations from a thread. Qt does not support this behavior, so that probably explains the freezing. If you search a little, there are many articles and examples that discuss how to properly use threads with Qt.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/292d1a51-6aae-4d2c-b7c9-6228dccf178b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Jan Sebastian

unread,
May 15, 2018, 12:58:23 PM5/15/18
to pyqtgraph
so, can I using this: https://groups.google.com/forum/#!topic/pyqtgraph/haiJsGhxTaQ as a example for my code if I didn't want using qTimer?

Jeremy Webster

unread,
May 15, 2018, 1:16:28 PM5/15/18
to pyqtgraph
To add to Luke's point

1. I suspect you should use QThread instead of python's threading.Thread, since QThread will provide signals and slots that will easily allow the thread to communicate back and forth with the GUI.

2.  Some tutorials will tell you to subclass QThread, but that seems to be bad advice, the correct way is to create a worker object, then add it to a QThread.

3. (For a C++ based explanation, but one that applies to pyqt as well https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ )

4. For a pyqt4 example, check out Matthew Levine's solution in this stackoverflow post... ( https://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt? )

Patrick

unread,
May 15, 2018, 10:36:25 PM5/15/18
to pyqtgraph
I had an issue similar to this which I solved while still using Python's threads/events etc rather than QThread (which was easier and less disruptive to my existing code). The trick was to make your own pyqtSignal and slots and only use those for communicating between the Python data acquisition thread and the Qt GUI thread when updates are required.

Demo example:

Jan Sebastian

unread,
May 22, 2018, 2:59:41 AM5/22/18
to pyqtgraph
Thank you for all of your explanation and examples, it's work


Pada Selasa, 15 Mei 2018 23.17.41 UTC+7, Luke Campagnola menulis:

Jan Sebastian

unread,
May 22, 2018, 2:59:56 AM5/22/18
to pyqtgraph
Thank you for all of your explanation and examples, it's work

Reply all
Reply to author
Forward
0 new messages