Skip to content

Make widgets.TextBox work also when embedding. #17108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2020

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Apr 12, 2020

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_()

and try to type "s" in the textbox.

@Stefan-Mitic's #17105 PR first noted the possibility that manager may be None (thanks!), but I think the solution here (disabling keymaps even in that case) is more robust.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

else:
toolmanager = self.ax.figure.canvas.manager.toolmanager
toolmanager.keypresslock.release(self)
self._on_stop_typing()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess nothing else would normally call it, but set self._on_stop_typing = None to gc it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

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_()
```
@anntzer anntzer force-pushed the textbox-embedding branch from 335f2fa to 1c6d5a1 Compare April 14, 2020 08:04
@timhoffm timhoffm merged commit 3297484 into matplotlib:master Apr 15, 2020
@QuLogic QuLogic added this to the v3.3.0 milestone Apr 15, 2020
@anntzer anntzer deleted the textbox-embedding branch April 15, 2020 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants