Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Mar 16, 2017

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

`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_()
```
@WeatherGod
Copy link
Member

Failure looks unrelated (stale tex cache lock on windows)

@WeatherGod WeatherGod merged commit 6757719 into matplotlib:master May 22, 2017
@anntzer anntzer deleted the blocking-input-for-unmanaged-figures branch May 22, 2017 15:28
@QuLogic QuLogic added this to the 2.1 (next point release) milestone May 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants