简体   繁体   English

PyQtGraph-如何关闭绘图窗口/关闭所有绘图窗口?

[英]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. 我尝试了pg.close(),但是没有用,在手册中找不到。 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. 所有Qt小部件都有一个close方法。

And you can close all windows using QApplication.closeAllWindows . 您可以使用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. 我本人遇到了这个问题,在阅读了pyqtgraph源代码后 ,发现需要与绘图本身一起关闭的窗口。 For example, pg.close() to close the plot and pg.win.close() to close the window containing the plot. 例如,使用pg.close()关闭绘图,然后使用pg.win.close()关闭包含绘图的窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM