Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1c6d5a1

Browse filesBrowse files
committed
Make widgets.TextBox work also when embedding.
When embedding, `canvas.manager` may be None, but we may still have registered the default key_press_handler (see e.g. the embedding_in_tk_sgskip.py example), so we still need to disable the keymap rcParams. In order to avoid duplicating the logic as to whether use toolmanager-cleanup in two places (and avoid things going out of sync between begin_typing and stop_typing), register the cleanup actions in begin_typing. Example: ``` from matplotlib.backend_bases import key_press_handler from matplotlib.backends.backend_qt5agg import FigureCanvas, NavigationToolbar2QT from matplotlib.backends.qt_compat import QtCore, QtWidgets from matplotlib.figure import Figure from matplotlib.widgets import TextBox class ApplicationWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() canvas = FigureCanvas(Figure(figsize=(5, 3))) canvas.setFocusPolicy(QtCore.Qt.StrongFocus) self.setCentralWidget(canvas) tb = NavigationToolbar2QT(canvas, self) self.addToolBar(tb) canvas.mpl_connect( "key_press_event", lambda event: key_press_handler(event, canvas, tb)) axbox = canvas.figure.add_axes([0.1, 0.05, 0.8, 0.075]) self._tb = TextBox(axbox, 'label') qapp = QtWidgets.QApplication([]) app = ApplicationWindow() app.show() qapp.exec_() ```
1 parent 065769b commit 1c6d5a1
Copy full SHA for 1c6d5a1

File tree

Expand file treeCollapse file tree

1 file changed

+22
-21
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+22
-21
lines changed

‎lib/matplotlib/widgets.py

Copy file name to clipboardExpand all lines: lib/matplotlib/widgets.py
+22-21Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -842,37 +842,38 @@ def _notify_change_observers(self):
842842

843843
def begin_typing(self, x):
844844
self.capturekeystrokes = True
845-
# Check for toolmanager handling the keypress
846-
if self.ax.figure.canvas.manager.key_press_handler_id is not None:
847-
# Disable command keys so that the user can type without
848-
# command keys causing figure to be saved, etc.
849-
self._restore_keymap = ExitStack()
845+
# Disable keypress shortcuts, which may otherwise cause the figure to
846+
# be saved, closed, etc., until the user stops typing. The way to
847+
# achieve this depends on whether toolmanager is in use.
848+
stack = ExitStack() # Register cleanup actions when user stops typing.
849+
self._on_stop_typing = stack.close
850+
toolmanager = getattr(
851+
self.ax.figure.canvas.manager, "toolmanager", None)
852+
if toolmanager is not None:
853+
# If using toolmanager, lock keypresses, and plan to release the
854+
# lock when typing stops.
855+
toolmanager.keypresslock(self)
856+
stack.push(toolmanager.keypresslock.release, self)
857+
else:
858+
# If not using toolmanager, disable all keypress-related rcParams.
850859
# Avoid spurious warnings if keymaps are getting deprecated.
851860
with cbook._suppress_matplotlib_deprecation_warning():
852-
self._restore_keymap.enter_context(
853-
mpl.rc_context({k: [] for k in mpl.rcParams
854-
if k.startswith('keymap.')}))
855-
else:
856-
self.ax.figure.canvas.manager.toolmanager.keypresslock(self)
861+
stack.enter_context(mpl.rc_context(
862+
{k: [] for k in mpl.rcParams if k.startswith("keymap.")}))
857863

858864
def stop_typing(self):
859-
notifysubmit = False
860-
# Because _notify_submit_users might throw an error in the user's code,
861-
# we only want to call it once we've already done our cleanup.
862865
if self.capturekeystrokes:
863-
# Check for toolmanager handling the keypress
864-
if self.ax.figure.canvas.manager.key_press_handler_id is not None:
865-
# since the user is no longer typing,
866-
# reactivate the standard command keys
867-
self._restore_keymap.close()
868-
else:
869-
toolmanager = self.ax.figure.canvas.manager.toolmanager
870-
toolmanager.keypresslock.release(self)
866+
self._on_stop_typing()
867+
self._on_stop_typing = None
871868
notifysubmit = True
869+
else:
870+
notifysubmit = False
872871
self.capturekeystrokes = False
873872
self.cursor.set_visible(False)
874873
self.ax.figure.canvas.draw()
875874
if notifysubmit:
875+
# Because _notify_submit_observers might throw an error in the
876+
# user's code, only call it once we've already done our cleanup.
876877
self._notify_submit_observers()
877878

878879
def position_cursor(self, x):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.