From 7d944363e67be0252996610dabebefff7efe13bd Mon Sep 17 00:00:00 2001 From: rgw Date: Sun, 23 Jun 2024 18:19:01 +0800 Subject: [PATCH] Fix repeated event binding on figure creation using class attribute This commit introduces a class attribute to track the connection status of the clipboard event handler, ensuring it's only bound once per figure. --- src/addcopyfighandler.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/addcopyfighandler.py b/src/addcopyfighandler.py index eb7e909..486b316 100644 --- a/src/addcopyfighandler.py +++ b/src/addcopyfighandler.py @@ -45,7 +45,7 @@ import matplotlib.backends import matplotlib.pyplot as plt -__version__ = '3.2.1' +__version__ = '3.2.2' __version_info__ = tuple(int(i) if i.isdigit() else i for i in __version__.split('.')) oldfig = plt.figure @@ -264,13 +264,19 @@ def copyfig(fig=None, *args, **kwargs): @wraps(plt.figure) def newfig(*args, **kwargs): + if not hasattr(newfig, "handler_connected"): + newfig.handler_connected = False + fig = oldfig(*args, **kwargs) def clipboard_handler(event): if event.key in ('ctrl+c', 'cmd+c'): copyfig() - fig.canvas.mpl_connect('key_press_event', clipboard_handler) + if not newfig.handler_connected: + fig.canvas.mpl_connect('key_press_event', clipboard_handler) + newfig.handler_connected = True + return fig