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.

Arjun Patel
  • 266
  • 5
  • 18

2 Answers2

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.

Community
  • 1
  • 1
101
  • 8,514
  • 6
  • 43
  • 69
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()

TaekYoung
  • 45
  • 7