Mattvers
Mattvers

Reputation: 46

Live data graph does not open and show any results Python

I have been trying to show a graph which is continuously updating (to simulate real time data visualization). For the graph I am using pyqtgraph in pyqt and everything works fine for a single plot. When I try to use QtCore.QTimer.singleShot(1,self.update()), the update function works but the graph and the window does not show (can not load). I tried to follow the following example: https://www.swharden.com/wp/2016-07-31-real-time-audio-monitor-with-pyqt/

class AppWindow(QDialog,test3.Ui_Dialog):
    def __init__(self,parent=None):
        pg.setConfigOption('background', 'w') #before loading widget
        super(AppWindow,self).__init__()
        self.setupUi(self)

    def update(self):
        print("icerde")
        t1=time.clock()
        points=100
        x=np.arange(points)
        data = np.sin(np.arange(points)/points*3*np.pi+time.time())
        C=pg.hsvColor(time.time()/5%1,alpha=.5)
        pen=pg.mkPen(color=C,width=10)
        self.graphicsView.plot(x,data,pen=pen,clear=True)
        self.repeatself.setChecked(True)
        QtCore.QTimer.singleShot(1,self.update())

if __name__=="__main__":
    app = QApplication(sys.argv)
    w = AppWindow()
    w.show()
    w.update()
    app.exec_()
    print("DONE")

GUI Part is:

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(1006, 771)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(640, 690, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setObjectName("buttonBox")
        #self.graphicsView = QtWidgets.QGraphicsView(Dialog)
        self.graphicsView = pg.PlotWidget(Dialog)
        self.graphicsView.setGeometry(QtCore.QRect(20, 30, 431, 281))
        self.graphicsView.setObjectName("graphicsView")
        self.repeatself = QtWidgets.QCheckBox(Dialog)
        self.repeatself.setGeometry(QtCore.QRect(490, 30, 70, 17))
        self.repeatself.setObjectName("repeatself")

        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)


    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.repeatself.setText(_translate("Dialog", "Repeat"))

Upvotes: 1

Views: 46

Answers (1)

Mattvers
Mattvers

Reputation: 46

Well I have solved the issue myself:

we need to modify the code as follows:

class AppWindow(QDialog,test3.Ui_Dialog):
    def __init__(self,parent=None):
        pg.setConfigOption('background', 'w') #before loading widget
        super(AppWindow,self).__init__()
        self.setupUi(self)


    def update(self):
        print("icerde")
        t1=time.clock()
        points=100
        x=np.arange(points)
        data = np.sin(np.arange(points)/points*3*np.pi+time.time())
        C=pg.hsvColor(time.time()/5%1,alpha=.5)
        pen=pg.mkPen(color=C,width=10)
        self.graphicsView.plot(x,data,pen=pen,clear=True)
        QtCore.QCoreApplication.processEvents()
        self.repeatself.setChecked(True)
        QtCore.QTimer.singleShot(1,self.update())

if __name__=="__main__":
    app = QApplication(sys.argv)
    w = AppWindow()
    w.show()
    w.update()
    app.exec_()
    print("DONE")

Upvotes: 1

Related Questions