2014-05-03 68 views

回答

1

所有Qt小部件都有一个close方法。

而且您可以使用QApplication.closeAllWindows关闭所有窗口。

+0

这就是我的想法 - 虽然奇怪的是这只是给我留下了空白的窗户而不是摧毁它们! – cjm2671

+0

如果关闭窗口内唯一的小部件,那么是的,您将留下一个空窗口。确保你正在窗口本身调用close()。 – Luke

0

下面显示的代码具有退出方法。

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)