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).
All Qt widgets have a close
method.
And you can close all windows using QApplication.closeAllWindows
.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With