Skip to content

Commit 0cf2d53

Browse files
authored
Merge pull request #25559 from anntzer/tmpguievent
Make guiEvent available only within the event handlers.
2 parents 9e52bc8 + ab6ecda commit 0cf2d53

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Accessing ``event.guiEvent`` after event handlers return
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated: for some GUI toolkits, it is unsafe to do so. In the
4+
future, ``event.guiEvent`` will be set to None once the event handlers return;
5+
you may separately stash the object at your own risk.

lib/matplotlib/backend_bases.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1257,11 +1257,26 @@ class Event:
12571257
def __init__(self, name, canvas, guiEvent=None):
12581258
self.name = name
12591259
self.canvas = canvas
1260-
self.guiEvent = guiEvent
1260+
self._guiEvent = guiEvent
1261+
self._guiEvent_deleted = False
12611262

12621263
def _process(self):
1263-
"""Generate an event with name ``self.name`` on ``self.canvas``."""
1264+
"""Process this event on ``self.canvas``, then unset ``guiEvent``."""
12641265
self.canvas.callbacks.process(self.name, self)
1266+
self._guiEvent_deleted = True
1267+
1268+
@property
1269+
def guiEvent(self):
1270+
# After deprecation elapses: remove _guiEvent_deleted; make guiEvent a plain
1271+
# attribute set to None by _process.
1272+
if self._guiEvent_deleted:
1273+
_api.warn_deprecated(
1274+
"3.8", message="Accessing guiEvent outside of the original GUI event "
1275+
"handler is unsafe and deprecated since %(since)s; in the future, the "
1276+
"attribute will be set to None after quitting the event handler. You "
1277+
"may separately record the value of the guiEvent attribute at your own "
1278+
"risk.")
1279+
return self._guiEvent
12651280

12661281

12671282
class DrawEvent(Event):

0 commit comments

Comments
 (0)