Python matplotlib动画关闭事件

Python matplotlib动画关闭事件,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我使用matplotlib中的animation.FuncAnimation来查看相机图片。我使用Python3.6。是否有可能将函数附加到关闭事件 我的目标是: 如果我关上窗户,我也想关上相机。我只想关闭动画窗口,而不是整个python应用程序。最好的方法是什么 from Class.LiveView import LiveView from Class.PixelFormat import PixelFormat import matplotlib.pyplot as plt import

我使用matplotlib中的animation.FuncAnimation来查看相机图片。我使用Python3.6。是否有可能将函数附加到关闭事件

我的目标是: 如果我关上窗户,我也想关上相机。我只想关闭动画窗口,而不是整个python应用程序。最好的方法是什么

from Class.LiveView import LiveView
from Class.PixelFormat import PixelFormat
import matplotlib.pyplot as plt
import matplotlib.animation as animation

class Viewer(object):
   """show picture"""
   def __init__ (self):
      self.cap = LiveView()
      self.cap.startcam()
      self.cap.autoExposureTime()

   def plotPicLive(self):
      self.cap.startGetPic(PixelFormat.Mono8)
      fig = plt.figure()
      frame = self.cap.getPic()
      fig.add_subplot(1,1,1)
      im = plt.imshow(frame, animated=True)
      def updatefig(*args):
         frame = self.cap.getPic()
         im.set_array(frame)
         return im
      ani = animation.FuncAnimation(fig,updatefig, interval=1)
      plt.show()

   def close(self):
      self.cap.stopPic()
      self.cap.close()
      self.cap.cleanCam()
这只是一个示例类

先谢谢你

Matplotlib有一个关闭事件。不幸的是,在中没有很好的文档记录,但是关于如何使用它还有很多问题。举个例子:

from __future__ import print_function
import matplotlib.pyplot as plt


def handle_close(evt):
    print('Closed Figure!')

fig = plt.figure()
fig.canvas.mpl_connect('close_event', handle_close)

plt.text(0.35, 0.5, 'Close Me!', dict(size=30))
plt.show()
在您的情况下,假设代码的其余部分工作正常,这可以直接用作

      # .....

      ani = animation.FuncAnimation(fig,updatefig, interval=1)
      fig.canvas.mpl_connect('close_event', self.close)
      plt.show()

   def close(self):
      self.cap.stopPic()
      self.cap.close()
      self.cap.cleanCam()
Matplotlib有一个关闭事件。不幸的是,在中没有很好的文档记录,但是关于如何使用它还有很多问题。举个例子:

from __future__ import print_function
import matplotlib.pyplot as plt


def handle_close(evt):
    print('Closed Figure!')

fig = plt.figure()
fig.canvas.mpl_connect('close_event', handle_close)

plt.text(0.35, 0.5, 'Close Me!', dict(size=30))
plt.show()
在您的情况下,假设代码的其余部分工作正常,这可以直接用作

      # .....

      ani = animation.FuncAnimation(fig,updatefig, interval=1)
      fig.canvas.mpl_connect('close_event', self.close)
      plt.show()

   def close(self):
      self.cap.stopPic()
      self.cap.close()
      self.cap.cleanCam()