6

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).

cjm2671
  • 18,348
  • 31
  • 102
  • 161

3 Answers3

2

All Qt widgets have a close method.

And you can close all windows using QApplication.closeAllWindows.

jesterjunk
  • 2,342
  • 22
  • 18
Luke
  • 11,374
  • 2
  • 48
  • 61
  • That's what I thought- although strangely this just leaves me with blank windows instead of destroying them! – cjm2671 May 03 '14 at 13:13
  • 1
    If you close the only widget inside a window, then yes, you would be left with an empty window. Make sure you are calling `close()` on the window itself. – Luke May 03 '14 at 15:48
1

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)
acs
  • 796
  • 2
  • 14
  • 33
1

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.

Austin
  • 11
  • 3