8000 In ginput(), don't call show() if we can't. by anntzer · Pull Request #8305 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

In ginput(), don't call show() if we can't. #8305

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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
In ginput(), don't call show() if we can't.
`ginput` works even for figures that are not created by `plt.figure()`
(and friends -- typically, that means we're embedding matplotlib in a
GUI app)... except that `ginput` tries to call `fig.show()` to raise the
figure, which would fail.

Instead, only call `fig.show` if the figure has a manager (i.e., was
created by `plt.figure`).

Example (click once to start point selection, then make further clicks
as in a normal `ginput` call):

```
import matplotlib; matplotlib.use("qt5agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *

class Win(QMainWindow):
    def __init__(self):
        super().__init__()
        self.fig = fig = Figure()
        canvas = FigureCanvasQTAgg(fig)
        self.setCentralWidget(canvas)
        fig.add_subplot(111)
        self._cid = fig.canvas.mpl_connect(
            "button_press_event", self._start_ginput)

    def _start_ginput(self, event):
        self.fig.canvas.mpl_disconnect(self._cid)
        # Workaround for now; patch makes this unnecessary.
        self.fig.show = lambda: None
        print(self.fig.ginput(-1, timeout=3))

app = QApplication([])
win = Win()
win.show()
app.exec_()
```
  • Loading branch information
anntzer committed Mar 16, 2017
commit a62e8be56de4415eb7586cfc7062f9a894d04e47
5 changes: 3 additions & 2 deletions lib/matplotlib/blocking_input.py
62E0
Original file line number Diff line numberDiff line change
Expand Up @@ -104,8 +104,9 @@ def __call__(self, n=1, timeout=30):
self.events = []
self.callbacks = []

# Ensure that the figure is shown
self.fig.show()
if hasattr(self.fig, "manager"):
# Ensure that the figure is shown, if we are managing it.
self.fig.show()

# connect the events to the on_event function call
for n in self.eventslist:
Expand Down
0