From 1b6bc8725838940cd6cc21508417022b97e62684 Mon Sep 17 00:00:00 2001 From: Josh Burnett Date: Thu, 5 Sep 2024 11:26:46 -0400 Subject: [PATCH] Fix issue when using the Agg backend on Windows :doesn't support get_window_title() --- README.md | 4 ++++ src/addcopyfighandler.py | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b3c0afa..a1f9b07 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,10 @@ Uses code & concepts from: Releases -------- +### 3.2.4: 2024-09-03 + +- Fix [issue when using the Agg backend on Windows](https://github.com/joshburnett/addcopyfighandler/issues/20), which doesn't support get_window_title(). + ### 3.2.3: 2024-09-03 - Fixed [an issue with copying when there are multiple figures](https://github.com/joshburnett/addcopyfighandler/issues/19). diff --git a/src/addcopyfighandler.py b/src/addcopyfighandler.py index 2359e6e..2468a0c 100644 --- a/src/addcopyfighandler.py +++ b/src/addcopyfighandler.py @@ -45,7 +45,7 @@ import matplotlib.backends import matplotlib.pyplot as plt -__version__ = '3.2.3' +__version__ = '3.2.4' __version_info__ = tuple(int(i) if i.isdigit() else i for i in __version__.split('.')) oldfig = plt.figure @@ -105,7 +105,11 @@ def copyfig(fig=None, format=None, *args, **kwargs): fig = fig_to_check break else: - fig_window_text = fig.canvas.manager.get_window_title() + try: + fig_window_text = fig.canvas.manager.get_window_title() + except AttributeError: + # A figure was specified, but the backend doesn't support get_window_title(). This happens w/ Agg. + fig_window_text = None if fig is None: raise AttributeError('No figure found!') @@ -138,7 +142,10 @@ def copyfig(fig=None, format=None, *args, **kwargs): win32clipboard.SetClipboardData(format_id, data) win32clipboard.CloseClipboard() - print(f'Figure copied: Window title="{fig_window_text}"') + if fig_window_text is not None: + print(f'Figure copied: Window title="{fig_window_text}"') + else: + print('Figure copied') elif 'qt' in backend.lower(): # Use Qt version from matplotlib.