2017-09-06 12 views
0

私は、matplotlibのanimation.FuncAnimationを使用してカメラの画像を表示します。私はPython 3.6を使用します。 closeイベントに関数を追加する可能性はありますか?matplotlib animation closeイベント

私の目標は: 私が窓を閉じると、カメラを閉じることもできます。私はちょうど全体の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() 

これはクラスの一例です。

ありがとうございます。

答えて

0

Matplotlibはclose_eventです。これは残念なことに、event handling guideには記載されていませんが、使用方法についてはan exampleがあります。例引用するには:あなたのケースでは

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() 
として使用することができ
関連する問題