[Matplotlib-users] Please advise...How to detect if a user has closed the matplotlib figure window?

Thomas Caswell tcaswell at gmail.com
Fri Aug 20 15:32:04 EDT 2021


What error did we use to raise?  I would not have expected that.

You can use the `close_event` to be notified what the window is closed (see
https://matplotlib.org/stable/gallery/event_handling/close_event.html).
You can probably also keep a reference to the Figure you create and query
its state.

>From this code snippet I can not tell if you are embedding Matplotlib in a
bigger GUI application or not.  If you are, you can rely on it's main loop
to be spinning so you do not actually need `plt.pause()`.

Tom

On Fri, Aug 20, 2021 at 3:07 PM Anthony Billings <ABillings at axustech.com>
wrote:

> Good day,
>
>
>
> With a previous version of matplotlib, I was able to plot 4 axes on a
> figure and update it, in a while loop, with plt.pause().
> If the figure window was closed by the user, the plt.pause() would signal
> an error and I was able to escape the while loop using try/except.
>
>             try
> :
> #
>
>                 plt.pause(0.05
> )
> #
>
>                 if gv.saveImage_UserCancel:
>
>                     H1_FinishingOptions.saveImage(temp_name=f"{
> Names.processName}", option=2
> )
> #
>
>             except:
> # This allows the closing of graph display window and going back to OED main menu. Variables and lists are reset for next scan.                                                                 #
>
>                 if
>  gv.multWafers_lastWaferDetected:
> #
>
>                     H1_FinishingOptions.UserCancelled(time_lst
> =H1_waferScan_time_lst, select='single'
> )
> #
>
>                     process.cprint('END'
> )
> #
>
>                     break
>
> #
>
>                 elif not
>  gv.multWafers_lastWaferDetected:
> #
>
>                     H1_FinishingOptions.UserCancelled(time_lst
> =H1_waferScan_time_lst, select='multiple'
> )
> #
>
>                     break
>
> #
>
>
>
> With the new release of matplotlib, I’m no longer triggering an error when
> the figure window is manually closed, and the program then stalls in a loop
> because of the plt.pause().
>
>   File
> "c:/Users/ABillings/AppData/Local/Programs/Python/Python37/MyPythonDocs/Endpoint-08.19.21(Samtec).py",
> line 12576, in epMain
>
>     plt.pause(0.05)
>
>   File
> "C:\Users\ABillings\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py",
> line 558, in pause
>
>     canvas.start_event_loop(interval)
>
>   File
> "C:\Users\ABillings\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py",
> line 385, in start_event_loop
>
>     self._master.mainloop()
>
>   File
> "C:\Users\ABillings\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py",
> line 1283, in mainloop
>
>     self.tk.mainloop(n)
>
> KeyboardInterrupt
>
>
>
>
>
> I’m now trying things like below, but to no avail. I need to somehow get
> the program to break the while loop when it detects a user has closed the
> figure window.
>
> Is there a method I can call to check whether the figure is still opened
> and make a decision based on that?
>
> That’s what I’m trying to do here, but the code stalls in the plt.pause()
> every time.
>
>             if not plt.fignum_exists(1):
>
>                 print(f"plt.get_axes() is False")
>
>                 break
>
>             else:
>
>                 print(plt.fignum_exists(1))
>
>                 # print(fig.get_axes())
>
>                 print(f"plt.get_axes() is True")
>
>                 # print(plt.get_fignums())
>
>                 # plt.show(block=False)
>
>                 print("Before Pause")
>
>                 plt.pause(0.05)
>
>                 print("After Pause")
>
>
>
> Please and thank you.
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>


-- 
Thomas Caswell
tcaswell at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/matplotlib-users/attachments/20210820/6fb67452/attachment-0001.html>


More information about the Matplotlib-users mailing list