tesla1060
tesla1060

Reputation: 2765

pyqtgraph real time updating graph not showing

I have tried the following code, that works in a single script file, I can see a chart being updated real time,

from PyQt4 import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time

app = QtGui.QApplication([])

pw = pg.plot()
timer = pg.QtCore.QTimer()


def update():
    x = np.random.normal(size=(100))
    y = np.random.normal(size=(100))
    pw.plot(x, y, clear=True)


timer.timeout.connect(update)
timer.start(0)

## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
    import sys

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

But if I put the updating real time part into a function that is called on a button click within a dialog, the chart shows nothing,

class TestDialog(QtGui.QDialog):
    def __init__(self, parent):
        super(TestDialog, self).__init__(parent, flags=QtCore.Qt.WindowMinimizeButtonHint|QtCore.Qt.WindowMaximizeButtonHint)

    self.resize(1000,618)

    self.chart_button = QtGui.QPushButton('Show chart', self)
    self.chart_button.clicked.connect(self.show_chart)
    vbox = QtGui.QVBoxLayout()
    vbox.addwidget(self.chart_button)
    self.setLayout(vbox)

    def show_chart(self):
        pw = pg.plot()
        timer = pg.QtCore.QTimer()

        def update():
            x = np.random.normal(size=(100))
            y = np.random.normal(size=(100))
            pw.plot(x, y, clear=True)

        timer.timeout.connect(update)
        timer.start(0)
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

If I set a breakpoint within update function, I found it is not being called in the second script, may I know why?

Upvotes: 2

Views: 3512

Answers (2)

eyllanesc
eyllanesc

Reputation: 243897

You only have to pass the parent to the timer. Change

timer = pg.QtCore.QTimer()

to

timer = pg.QtCore.QTimer(self)

Upvotes: 2

ymmx
ymmx

Reputation: 4967

The below code works for me:

# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import sys
from pyqtgraph.ptime import time



class TestDialog(QtGui.QMainWindow):
    def __init__(self, parent):
        super(TestDialog, self).__init__()
        self.parent=parent
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.resize(1000,618)
        self.vbox = QtGui.QVBoxLayout()

        self.chart_button = QtGui.QPushButton('Show chart', self)
        self.pw = pg.PlotWidget()
        self.vbox.addWidget(self.chart_button)
        self.vbox.addWidget(self.pw)
        self.centralWidget.setLayout(self.vbox)
        x = np.random.normal(size=(100))
        y = np.random.normal(size=(100))
        self.pw.plot(x, y,clear=True)
        self.chart_button.clicked.connect(self.show_chart)


    def update(self): 
        x = np.random.normal(size=(100))
        y = np.random.normal(size=(100))
        self.pw.plot(x, y, clear=True)
        QtCore.QCoreApplication.processEvents()


    def show_chart(self):
        self.timer = pg.QtCore.QTimer()
        self.timer.setSingleShot(False)
        self.timer.timeout.connect(self.update)
        self.timer.start(100)




## Start Qt event loop unless running in interactive mode.
def main():
    app = QtGui.QApplication(sys.argv)
    ex = TestDialog(app)
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

You could also keep the update inside the show_chart function if you want.

def show_chart(self):
    def update( ):
        x = np.random.normal(size=(100))
        y = np.random.normal(size=(100))
        self.pw.plot(x, y, clear=True)
        QtCore.QCoreApplication.processEvents()
    self.update = update
    self.timer = pg.QtCore.QTimer()
    self.timer.setSingleShot(False)
    self.timer.timeout.connect(self.update)
    self.timer.start(100)

Upvotes: -1

Related Questions