Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any resizing of the plot after plt.show results in an error when closing the window #24395

Closed
Tortar opened this issue Nov 8, 2022 · 4 comments · Fixed by #25158
Closed

Any resizing of the plot after plt.show results in an error when closing the window #24395

Tortar opened this issue Nov 8, 2022 · 4 comments · Fixed by #25158

Comments

@Tortar
Copy link
Contributor

Tortar commented Nov 8, 2022

Bug summary

After plt.show(), clicking on configure subplots and changing any parameter results in an error after closing the window.

Code for reproduction

import matplotlib.pyplot as plt
plt.plot([0,1],[0,1])
plt.show()
# after that, click on configure subplots and change any parameter, then close

Actual outcome

Traceback (most recent call last):
File "C:\Users\Bob\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\cbook_init_.py", line 307, in process
func(*args, **kwargs)
File "C:\Users\Bob\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\backend_bases.py", line 3290, in
"close_event", lambda e: manager.destroy())
File "C:\Users\Bob\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\backends_backend_tk.py", line 529, in destroy
self._window_dpi.trace_remove('write', self.window_dpi_cbname)
File "C:\Users\Bob\AppData\Local\Programs\Python\Python310\lib\tkinter_init
.py", line 455, in trace_remove
self._tk.deletecommand(cbname)
_tkinter.TclError: can't delete Tcl command

Expected outcome

Close without error

Additional information

No response

Operating system

Windows 10

Matplotlib Version

matplotlib-3.6.2

Matplotlib Backend

No response

Python version

3.10

Jupyter version

No response

Installation

pip

@tacaswell
Copy link
Member

I can confirm this, however I get the traceback (in IPython with %matplotlib tk):

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
    return self.func(*args)
  File "/usr/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 207, in filter_destroy
    CloseEvent("close_event", self)._process()
  File "/usr/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 1230, in _process
    self.canvas.callbacks.process(self.name, self)
  File "/usr/lib/python3.10/site-packages/matplotlib/cbook/__init__.py", line 312, in process
    self.exception_handler(exc)
  File "/usr/lib/python3.10/site-packages/matplotlib/cbook/__init__.py", line 96, in _exception_printer
    raise exc
  File "/usr/lib/python3.10/site-packages/matplotlib/cbook/__init__.py", line 307, in process
    func(*args, **kwargs)
  File "/usr/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 3290, in <lambda>
    "close_event", lambda e: manager.destroy())
  File "/usr/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 547, in destroy
    delayed_destroy()
  File "/usr/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 537, in delayed_destroy
    self.window.destroy()
  File "/usr/lib/python3.10/tkinter/__init__.py", line 2341, in destroy
    self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed

which is printed rather than raised in the process.

If you do plt.ioff() or plt.show(block=True) you can delay the error until the next time the event loop runs. My guess is that this is related to using a Figure for the UI and then some sort of race-condition on tear-down.

attn @richardsheridan

@tacaswell
Copy link
Member

Given that this is "just" printing, I think this is an annoyance rather than a critical bug.

@richardsheridan
Copy link
Contributor

I'm a bit confused by the current configure_subplots code, and I definitely didn't test it while working on shrinking memory leaks with the delayed_destroy strategy. The interaction of those callbacks and the figure canvas looks like a good place to start digging though.

@richardsheridan
Copy link
Contributor

Something clicked on this while working on a different bug. It now seems like a straightforward fix, just disconnect that callback on line 3290 when it's no longer needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment