2017-09-06 1 views
0

나는 matplotlib의 animation.FuncAnimation을 사용하여 카메라 사진을 봅니다. 나는 파이썬 3.6을 사용한다. close 이벤트에 함수를 추가 할 수 있습니까?matplotlib animation close 이벤트

내 목표는 입니다. 창을 닫으면 카메라를 닫고 싶습니다. 파이썬 애플리케이션 전체가 아닌 애니메이션의 창을 닫고 싶습니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

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입니다. 이 직접

 # ..... 

     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() 
로 사용될 수있다 (나머지 코드가 잘 작동한다는 가정하에) 귀하의 경우에는

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

: 예를 인용하려면