diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index bdbbd6ad6c77..7aa24e06ff8d 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -61,6 +61,8 @@ def _blit(argsid): the ``_blit_args`` dict, since arguments cannot be passed directly. """ photoimage, dataptr, offsets, bboxptr, comp_rule = _blit_args.pop(argsid) + if not photoimage.tk.call("info", "commands", photoimage): + return _tkagg.blit(photoimage.tk.interpaddr(), str(photoimage), dataptr, comp_rule, offsets, bboxptr) diff --git a/lib/matplotlib/tests/test_backend_tk.py b/lib/matplotlib/tests/test_backend_tk.py index 4d43e27aa4a4..eefaefbb023f 100644 --- a/lib/matplotlib/tests/test_backend_tk.py +++ b/lib/matplotlib/tests/test_backend_tk.py @@ -71,7 +71,7 @@ def test_blit(): import matplotlib.pyplot as plt import numpy as np import matplotlib.backends.backend_tkagg # noqa - from matplotlib.backends import _tkagg + from matplotlib.backends import _backend_tk, _tkagg fig, ax = plt.subplots() photoimage = fig.canvas._tkphoto @@ -93,6 +93,10 @@ def test_blit(): except ValueError: print("success") + # Test blitting to a destroyed canvas. + plt.close(fig) + _backend_tk.blit(photoimage, data, (0, 1, 2, 3)) + @_isolated_tk_test(success_count=1) def test_figuremanager_preserves_host_mainloop():