1

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.

2
  • How are you starting the GUI? Can you show code? Commented Dec 4, 2014 at 1:59
  • I just added the edit. Thanks for the reply! Commented Dec 4, 2014 at 2:26

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.