8000 Remove figure.show monkeypatch from GUI backends. · matplotlib/matplotlib@51ffd4b · GitHub
[go: up one dir, main page]

Skip to content

Commit 51ffd4b

Browse files
committed
Remove figure.show monkeypatch from GUI backends.
This also adds the warn kwarg to figure.show for non-gui backends.
1 parent faafa59 commit 51ffd4b

File tree

10 files changed

+52
-65
lines changed

10 files changed

+52
-65
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2403,6 +2403,8 @@ def key_press_handler(event, canvas, toolbar=None):
24032403
else:
24042404
a.set_navigate(i==n)
24052405

2406+
class NonGuiException(Exception):
2407+
pass
24062408

24072409
class FigureManagerBase:
24082410
"""
@@ -2436,8 +2438,11 @@ def __init__(self, canvas, num):
24362438
def show(self):
24372439
"""
24382440
For GUI backends, show the figure window and redraw.
2441+
For non-GUI backends, raise an exception to be caught
2442+
by :meth:`~matplotlib.figure.Figure.show`, for an
2443+
optional warning.
24392444
"""
2440-
pass
2445+
raise NonGuiException()
24412446

24422447
def destroy(self):
24432448
pass

lib/matplotlib/backends/backend_fltkagg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def handle(self, event):
143143
self._key=special_key[ikey]
144144
except:
145145
self._key=None
146-
146+
147147
# TODO: Handle ctrl, alt, super modifiers.
148148
FigureCanvasBase.key_press_event(self._source, self._key)
149149
return 1

lib/matplotlib/backends/backend_gtk.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,14 @@ def _get_key(self, event):
335335
key = chr(event.keyval)
336336
else:
337337
key = None
338-
338+
339339
for key_mask, prefix in (
340340
[gdk.MOD4_MASK, 'super'],
341-
[gdk.MOD1_MASK, 'alt'],
341+
[gdk.MOD1_MASK, 'alt'],
342342
[gdk.CONTROL_MASK, 'ctrl'],):
343343
if event.state & key_mask:
344344
key = '{}+{}'.format(prefix, key)
345-
345+
346346
return key
347347

348348
def configure_event(self, widget, event):
@@ -553,9 +553,6 @@ def __init__(self, canvas, num):
553553

554554
self.canvas.show()
555555

556-
# attach a show method to the figure for pylab ease of use
557-
self.canvas.figure.show = lambda *args: self.window.show()
558-
559556
self.vbox.pack_start(self.canvas, True, True)
560557

561558
self.toolbar = self._get_toolbar(canvas)

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,6 @@ def __init__(self, canvas, num):
378378

379379
self.canvas.show()
380380

381-
# attach a show method to the figure for pylab ease of use
382-
self.canvas.figure.show = lambda *args: self.window.show()
383-
384381
self.vbox.pack_start(self.canvas, True, True, 0)
385382

386383
self.toolbar = self._get_toolbar(canvas)
@@ -564,7 +561,7 @@ def configure_subplots(self, button):
564561

565562

566563
window = Gtk.Window()
567-
try:
564+
try:
568565
window.set_icon_from_file(window_icon)
569566
except (SystemExit, KeyboardInterrupt):
570567
# re-raise exit type Exceptions

lib/matplotlib/backends/backend_macosx.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,6 @@ def notify_axes_change(fig):
376376
if self.toolbar != None: self.toolbar.update()
377377
self.canvas.figure.add_axobserver(notify_axes_change)
378378

379-
# This is ugly, but this is what tkagg and gtk are doing.
380-
# It is needed to get ginput() working.
381-
self.canvas.figure.show = lambda *args: self.show()
382379
if matplotlib.is_interactive():
383380
self.show()
384381

lib/matplotlib/backends/backend_qt.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ def __init__( self, canvas, num ):
260260
if matplotlib.is_interactive():
261261
self.window.show()
262262

263-
# attach a show method to the figure for pylab ease of use
264-
self.canvas.figure.show = lambda *args: self.window.show()
265-
266263
def notify_axes_change( fig ):
267264
# This will be called whenever the current axes is changed
268265
if self.toolbar != None: self.toolbar.update()
@@ -330,7 +327,7 @@ def _init_toolbar( self ):
330327
continue
331328

332329
fname = os.path.join(basedir, image_file + '.ppm')
333-
image = qt.QPixmap()
330+
image = qt.QPixmap()
334331
image.load( fname )
335332

336333
button = qt.QPushButton( qt.QIconSet( image ), "", self )

lib/matplotlib/backends/backend_qt4.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,6 @@ def __init__( self, canvas, num ):
435435
if matplotlib.is_interactive():
436436
self.window.show()
437437

438-
# attach a show method to the figure for pylab ease of use
439-
self.canvas.figure.show = lambda *args: self.window.show()
440-
441438
def notify_axes_change( fig ):
442439
# This will be called whenever the current axes is changed
443440
if self.toolbar is not None:

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def new_figure_manager_given_figure(num, figure):
8585
"""
8686
_focus = windowing.FocusManager()
8787
window = Tk.Tk()
88-
88+
8989
if Tk.TkVersion >= 8.5:
9090
# put a mpl icon on the window rather than the default tk icon. Tkinter
9191
# doesn't allow colour icons on linux systems, but tk >=8.5 has a iconphoto
@@ -101,7 +101,7 @@ def new_figure_manager_given_figure(num, figure):
101101
except:
102102
# log the failure, but carry on
103103
verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1])
104-
104+
105105
canvas = FigureCanvasTkAgg(figure, master=window)
106106
figManager = FigureManagerTkAgg(canvas, num, window)
107107
if matplotlib.is_interactive():
@@ -199,7 +199,7 @@ class FigureCanvasTkAgg(FigureCanvasAgg):
199199
65439 : 'dec',
200200
65421 : 'enter',
201201
}
202-
202+
203203
_keycode_lookup = {
204204
262145: 'control',
205205
524320: 'alt',
@@ -254,7 +254,7 @@ def filter_destroy(evt):
254254

255255
self._master = master
256256
self._tkcanvas.focus_set()
257-
257+
258258
def resize(self, event):
259259
width, height = event.width, event.height
260260
if self._resize_callback is not None:
@@ -443,31 +443,31 @@ def _get_key(self, event):
443443
key = chr(val)
444444
else:
445445
key = None
446-
447-
# add modifier keys to the key string. Bit details originate from
446+
447+
# add modifier keys to the key string. Bit details originate from
448448
# http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
449-
# BIT_SHIFT = 0x001; BIT_CAPSLOCK = 0x002; BIT_CONTROL = 0x004;
450-
# BIT_LEFT_ALT = 0x008; BIT_NUMLOCK = 0x010; BIT_RIGHT_ALT = 0x080;
449+
# BIT_SHIFT = 0x001; BIT_CAPSLOCK = 0x002; BIT_CONTROL = 0x004;
450+
# BIT_LEFT_ALT = 0x008; BIT_NUMLOCK = 0x010; BIT_RIGHT_ALT = 0x080;
451451
# BIT_MB_1 = 0x100; BIT_MB_2 = 0x200; BIT_MB_3 = 0x400;
452-
# In general, the modifier key is excluded from the modifier flag,
453-
# however this is not the case on "darwin", so double check that
452+
# In general, the modifier key is excluded from the modifier flag,
453+
# however this is not the case on "darwin", so double check that
454454
# we aren't adding repeat modifier flags to a modifier key.
455-
modifiers = [(6, 'super', 'super'),
456-
(3, 'alt', 'alt'),
455+
modifiers = [(6, 'super', 'super'),
456+
(3, 'alt', 'alt'),
457457
(2, 'ctrl', 'control'),
458458
]
459459
if sys.platform == 'darwin':
460-
modifiers = [(3, 'super', 'super'),
461-
(4, 'alt', 'alt'),
460+
modifiers = [(3, 'super', 'super'),
461+
(4, 'alt', 'alt'),
462462
(2, 'ctrl', 'control'),
463463
]
464-
464+
465465
if key is not None:
466466
# note, shift is not added to the keys as this is already accounted for
467467
for bitmask, prefix, key_name in modifiers:
468-
if event.state & (1 << bitmask) and key_name not in key:
468+
if event.state & (1 << bitmask) and key_name not in key:
469469
key = '{}+{}'.format(prefix, key)
470-
470+
471471
return key
472472

473473
def key_press(self, event):
@@ -542,9 +542,6 @@ def notify_axes_change(fig):
542542
if self.toolbar != None: self.toolbar.update()
543543
self.canvas.figure.add_axobserver(notify_axes_change)
544544

545-
# attach a show method to the figure for pylab ease of use
546-
self.canvas.figure.show = lambda *args: self.show()
547-
548545
def resize(self, width, height=None):
549546
# before 09-12-22, the resize method takes a single *event*
550547
# parameter. On the other hand, the resize method of other
@@ -852,14 +849,14 @@ def _init_toolbar(self):
852849

853850
for text, tooltip_text, image_file, callback in self.toolitems:
854851
if text is None:
855-
# spacer, unhandled in Tk
852+
# spacer, unhandled in Tk
856853
pass
857854
else:
858855
button = self._Button(text=text, file=image_file,
859856
command=getattr(self, callback))
860857
if tooltip_text is not None:
861858
ToolTip.createToolTip(button, tooltip_text)
862-
859+
863860
self.message = Tk.StringVar(master=self)
864861
self._message_label = Tk.Label(master=self, textvariable=self.message)
865862
self._message_label.pack(side=Tk.RIGHT)
@@ -954,7 +951,7 @@ def leave(event):
954951
toolTip.hidetip()
955952
widget.bind('<Enter>', enter)
956953
widget.bind('<Leave>', leave)
957-
954+
958955
def __init__(self, widget):
959956
self.widget = widget
960957
self.tipwindow = None

lib/matplotlib/backends/backend_wx.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,8 @@ def Copy_to_Clipboard(self, event=None):
778778
"copy bitmap of canvas to system clipboard"
779779
bmp_obj = wx.BitmapDataObject()
780780
bmp_obj.SetBitmap(self.bitmap)
781-
782-
if not wx.TheClipboard.IsOpened():
781+
782+
if not wx.TheClipboard.IsOpened():
783783
open_success = wx.TheClipboard.Open()
784784
if open_success:
785785
wx.TheClipboard.SetData(bmp_obj)
@@ -1251,7 +1251,7 @@ def _get_key(self, evt):
12511251
key = None
12521252

12531253
for meth, prefix in (
1254-
[evt.AltDown, 'alt'],
1254+
[evt.AltDown, 'alt'],
12551255
[evt.ControlDown, 'ctrl'], ):
12561256
if meth():
12571257
key = '{}+{}'.format(prefix, key)
@@ -1421,7 +1421,7 @@ def _create_wx_app():
14211421
# retain a reference to the app object so it does not get garbage
14221422
# collected and cause segmentation faults
14231423
_create_wx_app.theWxApp = wxapp
1424-
1424+
14251425

14261426
def draw_if_interactive():
14271427
"""
@@ -1521,7 +1521,7 @@ def __init__(self, num, fig):
15211521
self.Fit()
15221522

15231523
self.canvas.SetMinSize((2, 2))
1524-
1524+
15251525
# give the window a matplotlib icon rather than the stock one.
15261526
# This is not currently working on Linux and is untested elsewhere.
15271527
#icon_path = os.path.join(matplotlib.rcParams['datapath'],
@@ -1603,12 +1603,6 @@ def notify_axes_change(fig):
16031603
if self.tb != None: self.tb.update()
16041604
self.canvas.figure.add_axobserver(notify_axes_change)
16051605

1606-
def showfig(*args):
1607-
frame.Show()
1608-
1609-
# attach a show method to the figure
1610-
self.canvas.figure.show = showfig
1611-
16121606
def show(self):
16131607
self.frame.Show()
16141608

@@ -1882,7 +1876,7 @@ def save_figure(self, *args):
18821876
os.path.join(dirname, filename), format=format)
18831877
except Exception as e:
18841878
error_msg_wx(str(e))
1885-
1879+
18861880
def set_cursor(self, cursor):
18871881
cursor =wx.StockCursor(cursord[cursor])
18881882
self.canvas.SetCursor( cursor )

lib/matplotlib/figure.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from matplotlib.text import Text, _process_text_args
4343
from matplotlib.transforms import (Affine2D, Bbox, BboxTransformTo,
4444
TransformedBbox)
45-
45+
from matplotlib.backend_bases import NonGuiException
4646

4747
docstring.interpd.update(projection_names = get_projection_names())
4848

@@ -338,19 +338,25 @@ def _setup_canvas(self):
338338
backend_mod = mbackends.pylab_setup()[0]
339339
return backend_mod.FigureCanvas(self)
340340

341-
def show(self):
341+
def show(self, warn=True):
342342
"""
343343
If using a GUI backend, display the figure window.
344344
345-
For non-GUI backends, this does nothing.
345+
For non-GUI backends, this does nothing, in which case
346+
a warning will be issued if *warn* is True.
346347
"""
347348
manager = getattr(self.canvas, 'manager')
348349
if manager is not None:
349-
manager.show()
350-
import warnings
351-
warnings.warn(
352-
"matplotlib is currently using a non-GUI backend, "
353-
"so can not show the figure")
350+
try:
351+
manager.show()
352+
return
353+
except NonGuiException:
354+
pass
355+
if warn:
356+
import warnings
357+
warnings.warn(
358+
"matplotlib is currently using a non-GUI backend, "
359+
"so cannot show the figure")
354360

355361
def _get_axes(self):
356362
return self._axstack.as_list()

0 commit comments

Comments
 (0)
0