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

五迷三道 提交于 2019-12-10 15:44:32

问题


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


回答1:


All Qt widgets have a close method.

And you can close all windows using QApplication.closeAllWindows.




回答2:


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)



回答3:


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.



来源:https://stackoverflow.com/questions/23444036/pyqtgraph-how-do-i-close-a-plot-window-close-all-plot-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!