Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQtGraph - how do I close a plot window/close all plot windows?

I tried pg.close() but that didn't work, couldn't find it in the manual. I produce plots in a loop, so I'd like to close them all at the end of each loop (instead it pops a new window open until my computer goes nuts).

like image 664
cjm2671 Avatar asked May 03 '14 11:05

cjm2671


3 Answers

All Qt widgets have a close method.

And you can close all windows using QApplication.closeAllWindows.

like image 87
Luke Avatar answered Nov 20 '22 04:11

Luke


The code shown below has a exit method.

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np
import time
import sys

class guiThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)
        self.status=True
        self.range=100

        #Add exit button connect and define a exit function like self.stop
        self.app = QtGui.QApplication(sys.argv)
        self.app.aboutToQuit.connect(self.stop)

        self.win = pg.GraphicsWindow(title="Example")
        self.win.resize(500,400)
        pg.setConfigOptions(antialias=True)
        self.px = self.win.addPlot(title="X plot")
        self.ckx = self.px.plot(pen='y')
        self.cdx = self.px.plot(pen='r')
        self.px.setXRange(0, self.range)
        self.px.setYRange(-180, 180)
        self.px.showGrid(x=True, y=True)
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.updateplot)
        self.timer.start(0.001)
        self.kx=np.zeros(self.range)
        self.dx=np.zeros(self.range)
    def updateplot(self):
        self.ckx.setData(self.kx)
        self.cdx.setData(self.dx)
    def append(self,sin):
        self.kx=np.roll(self.kx,-1)
        self.kx[-1]=sin[0]
        self.dx=np.roll(self.dx,-1)
        self.dx[-1]=int(sin[1])
    def stop(self):
        print "Exit" #exiy function
        self.status=False
        sys.exit()
    def run(self):
        print "run"
        while self.status:
            sin=np.random.randint(-180,180,2)
            print sin
            self.append(sin) 
            time.sleep(0.01)
like image 35
acs Avatar answered Nov 20 '22 05:11

acs


I had this issue myself and after reading through the pyqtgraph source code, found out the window needed to be closed along with the plot itself. For example, pg.close() to close the plot and pg.win.close() to close the window containing the plot.

like image 1
Austin Avatar answered Nov 20 '22 06:11

Austin