Skip to content

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

Closed

Description

@John1231983

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

Activity

  1. ImportanceOfBeingErnest commented on Jan 22, 2018

    @ImportanceOfBeingErnest
    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?

  2. added this to the milestone on Jan 22, 2018
  3. anntzer commented on Jan 22, 2018

    @anntzer
    Contributor

    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.

  4. anntzer commented on Jan 28, 2018

    @anntzer
    Contributor

    Closed by #10292.

  5. modified the milestones: , v2.2.0 on Feb 12, 2018
  6. added a commit that references this issue on Nov 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions