Skip to content

_tkinter.TclError: can't invoke "wm" command: application has been destroyed #10287

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

Closed
John1231983 opened this issue Jan 22, 2018 · 3 comments
Closed
Labels
Milestone

Comments

@John1231983
Copy link

Install
sudo python -mpip install -U matplotlib

Successfully installed matplotlib-2.1.2

Code

    plt.show()
    fig.savefig('./result.png',bbox_inches='tight')  # save the figure to file
    plt.close(fig)  # close the figure

OS: Ubuntu 16.04,
The error:

fig.savefig('./result.png',bbox_inches='tight')  # save the figure to file
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1834, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 2188, in print_figure
    self.figure.dpi = dpi
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 436, in _set_dpi
    self.set_size_inches(w, h, forward=forward)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 745, in set_size_inches
    manager.resize(int(canvasw), int(canvash))
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 540, in resize
    self.canvas._tkcanvas.master.geometry("%dx%d" % (width, height))
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1667, in wm_geometry
    return self.tk.call('wm', 'geometry', self._w, newGeometry)
_tkinter.TclError: can't invoke "wm" command: application has been destroyed
@ImportanceOfBeingErnest
Copy link
Member

Apparently the tk backend needs the figure window to be able to save the figure inside of it. Hence using the tk backend, the following fails.

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

fig = plt.figure()
plt.plot([1,2,3])

plt.show()
fig.savefig("test.png")

The usual way to circumvent this problem is to first save the figure and than show it. The following works as expected:

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

fig = plt.figure()
plt.plot([1,2,3])

fig.savefig("test.png")
plt.show()

Interestingly this is really a problem of the Tk backend. Using the Qt backend, it is possible to save the figure, even if the window does not exist anymore. The following works:

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

fig = plt.figure()
plt.plot([1,2,3])

plt.show()
fig.savefig("test.png")

So the issue here, if any, is probably rather: Is it really necessary for the Tk backend to have a window present in order to save a figure?

@tacaswell tacaswell added this to the v2.2 milestone Jan 22, 2018
@anntzer
Copy link
Contributor

anntzer commented Jan 22, 2018

I think this is an issue similar to #8736; we should not be using the same figure manager to save the plot but just instantiate a new one.

@anntzer
Copy link
Contributor

anntzer commented Jan 28, 2018

Closed by #10292.

@anntzer anntzer closed this as completed Jan 28, 2018
@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018
billbrod added a commit to plenoptic-org/plenoptic that referenced this issue Nov 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants