Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQtGraph opening then closing straight away

I'm running some basic code from the documentation

import pyqtgraph as pg
import numpy as np
x = np.arange(1000)
y = np.random.normal(size=(3, 1000))
plotWidget = pg.plot(title="Three plot curves")
for i in range(3):
    plotWidget.plot(x, y[i], pen=(i,3))

But for some reason the window open then closes straight away, i just see a flicker of it. Is there some sort of function in which i can keep the window open?

like image 477
Definity Avatar asked Jun 19 '17 13:06

Definity


1 Answers

You can keep the window open by creating a QApplication at the beginning of the script and then calling its exec_() method at the end of your script, like this:

import pyqtgraph as pg
import numpy as np
import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)  # Create QApplication ***

x = np.arange(1000)
y = np.random.normal(size=(3, 1000))
plotWidget = pg.plot(title="Three plot curves")
for i in range(3):
    plotWidget.plot(x, y[i], pen=(i, 3))

# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        app.exec_()  # Start QApplication event loop ***

I put *** on the key lines.

like image 118
EL_DON Avatar answered Nov 30 '22 22:11

EL_DON