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

Error when closing first of several pyplot figures in TkAgg #12703

Closed
ImportanceOfBeingErnest opened this issue Nov 1, 2018 · 2 comments · Fixed by #12707
Closed

Error when closing first of several pyplot figures in TkAgg #12703

ImportanceOfBeingErnest opened this issue Nov 1, 2018 · 2 comments · Fixed by #12707

Comments

@ImportanceOfBeingErnest
Copy link
Member

Bug report

Bug summary

Closing the first of two pyplot figures in TkAgg backend, throws an error.

Code for reproduction

import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt

fig1 = plt.figure()
plt.plot([1,2], 'o')

fig2 = plt.figure()
plt.plot([2,1], 'o')

plt.close(fig1)

plt.show()

Actual outcome

in py2 (2.2.3):

Traceback (most recent call last):
  File "D:\xxx\test\closefigureserror.py", line 19, in <module>
    plt.show()
  File "c:\***\lib\site-packages\matplotlib\pyplot.py", line 253, in show
    return _show(*args, **kw)
  File "c:\***\lib\site-packages\matplotlib\backend_bases.py", line 208, in show
    cls.mainloop()
  File "c:\***\lib\site-packages\matplotlib\backends\_backend_tk.py", line 1073, in mainloop
    Tk.mainloop()
  File "c:\***\lib\lib-tk\Tkinter.py", line 414, in mainloop
    _default_root.tk.mainloop(n)
AttributeError: 'NoneType' object has no attribute 'tk'

in py3 (master):

Traceback (most recent call last):
  File "D:\xxx\test\closefigureserror.py", line 19, in <module>
    plt.show()
  File "d:\***\matplotlib\lib\matplotlib\pyplot.py", line 254, in show
    return _show(*args, **kw)
  File "d:\***\matplotlib\lib\matplotlib\backend_bases.py", line 3275, in show
    cls.mainloop()
  File "d:\***\matplotlib\lib\matplotlib\backends\_backend_tk.py", line 1040, in mainloop
    Tk.mainloop()
  File "D:\***\Miniconda3\envs\mplgit\lib\tkinter\__init__.py", line 557, in mainloop
    _default_root.tk.mainloop(n)
AttributeError: 'NoneType' object has no attribute 'tk'

The code works fine in Qt4Agg (with py2) or Qt5Agg (with py3)

Expected outcome

No error.

Matplotlib version

  • Operating system: Win 8
  • Matplotlib version: 2.2.3 / master
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 2.7 / 3.6
  • Tcl/Tk version: 8.6
@anntzer
Copy link
Contributor

anntzer commented Nov 1, 2018

This can be repro'd without mpl:

import tkinter as tk
t1 = tk.Tk()
t2 = tk.Tk()
t1.destroy()
tk.mainloop()

I guess the correct fix is to call mainloop() on the first Tk() that's still alive...

@anntzer
Copy link
Contributor

anntzer commented Nov 1, 2018

Looks like

diff --git i/lib/matplotlib/backends/_backend_tk.py w/lib/matplotlib/backends/_backend_tk.py
index a11e65409..c88ad48b4 100644
--- i/lib/matplotlib/backends/_backend_tk.py
+++ w/lib/matplotlib/backends/_backend_tk.py
@@ -1037,4 +1037,6 @@ class _BackendTk(_Backend):
 
     @staticmethod
     def mainloop():
-        tk.mainloop()
+        managers = Gcf.get_all_fig_managers()
+        if managers:
+            managers[0].window.mainloop()

fixes the problem.
If that works for you, can to make a PR out of it?

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

Successfully merging a pull request may close this issue.

3 participants