title: OpenCV Python: How to detect if a window is closed?
body: <p>I have the following code:</p>
  
  <pre><code>total_frames = 50
  cv2.cv.NamedWindow("Dragonfly Simulation")
  cv2.cv.StartWindowThread()
  for i in range(total_frames):
      # do stuff
      img_name = # something
      img = cv2.cv.LoadImage(img_name)
      cv2.cv.ShowImage("Dragonfly Simulation", img)
      cv2.cv.WaitKey(2)
  cv2.cv.DestroyWindow("Dragonfly Simulation")
  cv2.cv.WaitKey(1)
  # rest of code
  </code></pre>
  
  <p>So what does it do:</p>
  
  <ol>
  <li>Opens a window</li>
  <li>In a loop, shows an image on the window</li>
  <li>Once finished, closes the window</li>
  <li>Runs the rest of the code</li>
  </ol>
  
  <p>However in this case I have the <code>total_frame</code> given before. I don't want that.</p>
  
  <p>Instead, I want a code that does the following:</p>
  
  <ol>
  <li>Opens a window</li>
  <li>In a loop, shows an image on the window</li>
  <li>Waits for the user to close that window</li>
  <li>When the user closes that window, exit loop, goes on with the rest of the code.</li>
  </ol>
  
  <p>However, I cannot find a function in OpenCV that can detect when user closes a window. Can anyone suggest a workaround please?</p>