I have an opencv python gui. When I click the "x" in the top right, my GUI closes but my program does not exit. How can I make the program exit when I click the "x" button?
I start my gui with:
cv2.namedWindow('frame')
Thank you.
I have an opencv python gui. When I click the "x" in the top right, my GUI closes but my program does not exit. How can I make the program exit when I click the "x" button?
I start my gui with:
cv2.namedWindow('frame')
Thank you.
The named window is just a window, it doesn't represent the whole program, so closing it just closes the window. You will likely need to make a GUI that is capable of catching close events (e.g. using PyQt). I don't think you can easily detect when a named window is closed, and if not then you won't be able to run an exit function and close your program.
Edit: it is possible (see https://stackoverflow.com/questions/9321389/how-to-check-if-an-opencv-window-is-closed, but that doesn't look like a nice path to go down.
I don't know whether you using interative shell or not but this method will work both situations. You can check if the cv2 window is closed or not with this.
# after open that image window
try:
cv2.getWindowProperty('frame', 0)
except cv2.error:
print("exit program")
sys.exit()