Ask Your Question
0

Detect when the imshow window is closed using X by user

asked 2019-05-28 19:01:48 -0600

Santhosh1 gravatar image

updated 2019-05-28 19:24:59 -0600

I would like to know if the user has closed the imshow window using on x on the right top corner on the window

Simple Code name simplegui.py

import cv2
import numpy as np

fresh_image = np.ones((480,640),np.uint8)

cv2.imshow('image',fresh_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

On clicking x of imshow window the python code doesn't stop as seen in the screens shot

image description

image description

I would like to python code to stop as soon as user closes the imshow using x on the right top corner of the window

Anyone help would be helpful.

Thank you for your time.

PS: OpenCV Python: How to detect if a window is closed? non of these solutions work

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-05-29 07:06:51 -0600

Lucy8 gravatar image

Put this before imshow

if cv2.waitKey(1) & 0xFF == ord('q'):
    cv2.destroyAllWindows()

Then if you hit q on your keyboard while imshow window is active it will close.

Or you can close it with an arbitrary key

import cv2
import numpy as np

fresh_image = np.ones((480,640),np.uint8)

if cv2.waitKey(1):
    cv2.destroyAllWindows()

cv2.imshow('image',fresh_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

This is the standard way that we all use, but doesn't show how to close a window by clicking X. It would be really nice to have this feature. When my users use applications it is embarassing to always be like "don't click X it will ruin everything even though it is there for some reason."

neuronet gravatar imageneuronet ( 2020-05-12 12:56:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-05-28 19:01:48 -0600

Seen: 12,687 times

Last updated: May 29 '19