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

Skip to content

Commit 1671be1

Browse files
committed
wx_compat is no more.
1 parent 8af1fac commit 1671be1

File tree

6 files changed

+112
-250
lines changed

6 files changed

+112
-250
lines changed

doc/api/next_api_changes/2018-02-15-AL-deprecations.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The following modules are deprecated:
55
- :mod:`matplotlib.compat.subprocess`. This was a python 2 workaround, but all
66
the functionality can now be found in the python 3 standard library
77
:mod:`subprocess`.
8+
- :mod:`matplotlib.backends.wx_compat`. Python 3 is only compatible with
9+
wxPython 4, so support for wxPython 3 or earlier can be dropped.
810

911
The following classes, methods, and functions are deprecated:
1012

examples/user_interfaces/mathtext_wx_sgskip.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import matplotlib
1111
matplotlib.use("WxAgg")
1212
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
13-
from matplotlib.backends.backend_wx import NavigationToolbar2Wx, wxc
13+
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
1414
from matplotlib.figure import Figure
1515
import numpy as np
1616

@@ -27,7 +27,7 @@
2727

2828
def mathtext_to_wxbitmap(s):
2929
ftimage, depth = mathtext_parser.parse(s, 150)
30-
return wxc.BitmapFromBuffer(
30+
return wx.Bitmap.FromBufferRGBA(
3131
ftimage.get_width(), ftimage.get_height(),
3232
ftimage.as_rgba_str())
3333
############################################################
@@ -43,7 +43,6 @@ def mathtext_to_wxbitmap(s):
4343
class CanvasFrame(wx.Frame):
4444
def __init__(self, parent, title):
4545
wx.Frame.__init__(self, parent, -1, title, size=(550, 350))
46-
self.SetBackgroundColour(wxc.NamedColour("WHITE"))
4746

4847
self.figure = Figure()
4948
self.axes = self.figure.add_subplot(111)
@@ -71,7 +70,7 @@ def __init__(self, parent, title):
7170
bm = mathtext_to_wxbitmap(mt)
7271
item = wx.MenuItem(menu, 1000 + i, " ")
7372
item.SetBitmap(bm)
74-
menu.AppendItem(item)
73+
menu.Append(item)
7574
self.Bind(wx.EVT_MENU, self.OnChangePlot, item)
7675
menuBar.Append(menu, "&Functions")
7776

examples/user_interfaces/wxcursor_demo_sgskip.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
11-
from matplotlib.backends.backend_wx import NavigationToolbar2Wx, wxc
11+
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
1212
from matplotlib.figure import Figure
1313
import numpy as np
1414

@@ -17,10 +17,7 @@
1717

1818
class CanvasFrame(wx.Frame):
1919
def __init__(self, ):
20-
wx.Frame.__init__(self, None, -1,
21-
'CanvasFrame', size=(550, 350))
22-
23-
self.SetBackgroundColour(wxc.NamedColour("WHITE"))
20+
wx.Frame.__init__(self, None, -1, 'CanvasFrame', size=(550, 350))
2421

2522
self.figure = Figure()
2623
self.axes = self.figure.add_subplot(111)
@@ -33,7 +30,8 @@ def __init__(self, ):
3330
self.figure_canvas = FigureCanvas(self, -1, self.figure)
3431

3532
# Note that event is a MplEvent
36-
self.figure_canvas.mpl_connect('motion_notify_event', self.UpdateStatusBar)
33+
self.figure_canvas.mpl_connect(
34+
'motion_notify_event', self.UpdateStatusBar)
3735
self.figure_canvas.Bind(wx.EVT_ENTER_WINDOW, self.ChangeCursor)
3836

3937
self.sizer = wx.BoxSizer(wx.VERTICAL)
@@ -49,14 +47,12 @@ def __init__(self, ):
4947
self.toolbar.Show()
5048

5149
def ChangeCursor(self, event):
52-
self.figure_canvas.SetCursor(wxc.StockCursor(wx.CURSOR_BULLSEYE))
50+
self.figure_canvas.SetCursor(wx.Cursor(wx.CURSOR_BULLSEYE))
5351

5452
def UpdateStatusBar(self, event):
5553
if event.inaxes:
56-
x, y = event.xdata, event.ydata
57-
self.statusBar.SetStatusText(("x= " + str(x) +
58-
" y=" + str(y)),
59-
0)
54+
self.statusBar.SetStatusText(
55+
"x={} y={}".format(event.xdata, event.ydata))
6056

6157

6258
class App(wx.App):

lib/matplotlib/backends/backend_wx.py

Lines changed: 72 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
"""
2-
A wxPython backend for matplotlib, based (very heavily) on
3-
backend_template.py and backend_gtk.py
2+
A wxPython backend for matplotlib.
43
5-
Author: Jeremy O'Donoghue ([email protected])
6-
7-
Derived from original copyright work by John Hunter
8-
9-
10-
Copyright (C) Jeremy O'Donoghue & John Hunter, 2003-4
11-
12-
License: This work is licensed under a PSF compatible license. A copy
13-
should be included with this source code.
4+
Originally contributed by Jeremy O'Donoghue ([email protected]) and John
5+
146
7+
Copyright (C) Jeremy O'Donoghue & John Hunter, 2003-4.
158
"""
16-
from __future__ import (absolute_import, division, print_function,
17-
unicode_literals)
189

1910
import six
2011

21-
import sys
22-
import os
2312
import os.path
2413
import math
25-
import weakref
14+
import sys
2615
import warnings
16+
import weakref
2717

2818
import matplotlib
2919
from matplotlib.backend_bases import (
@@ -39,7 +29,6 @@
3929
from matplotlib.widgets import SubplotTool
4030
from matplotlib import cbook, rcParams, backend_tools
4131

42-
from . import wx_compat as wxc
4332
import wx
4433

4534
# Debugging settings here...
@@ -173,14 +162,45 @@ class RendererWx(RendererBase):
173162
# describes the colour and weight of any lines drawn, and a wxBrush
174163
# which describes the fill colour of any closed polygon.
175164

176-
fontweights = wxc.fontweights
177-
fontangles = wxc.fontangles
165+
# Font styles, families and weight.
166+
fontweights = {
167+
100: wx.FONTWEIGHT_LIGHT,
168+
200: wx.FONTWEIGHT_LIGHT,
169+
300: wx.FONTWEIGHT_LIGHT,
170+
400: wx.FONTWEIGHT_NORMAL,
171+
500: wx.FONTWEIGHT_NORMAL,
172+
600: wx.FONTWEIGHT_NORMAL,
173+
700: wx.FONTWEIGHT_BOLD,
174+
800: wx.FONTWEIGHT_BOLD,
175+
900: wx.FONTWEIGHT_BOLD,
176+
'ultralight': wx.FONTWEIGHT_LIGHT,
177+
'light': wx.FONTWEIGHT_LIGHT,
178+
'normal': wx.FONTWEIGHT_NORMAL,
179+
'medium': wx.FONTWEIGHT_NORMAL,
180+
'semibold': wx.FONTWEIGHT_NORMAL,
181+
'bold': wx.FONTWEIGHT_BOLD,
182+
'heavy': wx.FONTWEIGHT_BOLD,
183+
'ultrabold': wx.FONTWEIGHT_BOLD,
184+
'black': wx.FONTWEIGHT_BOLD,
185+
}
186+
fontangles = {
187+
'italic': wx.FONTSTYLE_ITALIC,
188+
'normal': wx.FONTSTYLE_NORMAL,
189+
'oblique': wx.FONTSTYLE_SLANT,
190+
}
178191

179-
# wxPython allows for portable font styles, choosing them appropriately
180-
# for the target platform. Map some standard font names to the portable
181-
# styles
192+
# wxPython allows for portable font styles, choosing them appropriately for
193+
# the target platform. Map some standard font names to the portable styles.
182194
# QUESTION: Is it be wise to agree standard fontnames across all backends?
183-
fontnames = wxc.fontnames
195+
fontnames = {
196+
'Sans': wx.FONTFAMILY_SWISS,
197+
'Roman': wx.FONTFAMILY_ROMAN,
198+
'Script': wx.FONTFAMILY_SCRIPT,
199+
'Decorative': wx.FONTFAMILY_DECORATIVE,
200+
'Modern': wx.FONTFAMILY_MODERN,
201+
'Courier': wx.FONTFAMILY_MODERN,
202+
'courier': wx.FONTFAMILY_MODERN,
203+
}
184204

185205
def __init__(self, bitmap, dpi):
186206
"""
@@ -285,7 +305,7 @@ def draw_image(self, gc, x, y, im):
285305
w = self.width
286306
h = self.height
287307
rows, cols = im.shape[:2]
288-
bitmap = wxc.BitmapFromBuffer(cols, rows, im.tostring())
308+
bitmap = wx.Bitmap.FromBufferRGBA(cols, rows, im.tostring())
289309
gc = self.get_gc()
290310
gc.select()
291311
gc.gfx_ctx.DrawBitmap(bitmap, int(l), int(self.height - b),
@@ -611,27 +631,8 @@ def __init__(self, parent, id, figure):
611631

612632
wx.Panel.__init__(self, parent, id, size=wx.Size(w, h))
613633

614-
def do_nothing(*args, **kwargs):
615-
warnings.warn(
616-
"could not find a setinitialsize function for backend_wx; "
617-
"please report your wxpython version=%s "
618-
"to the matplotlib developers list" %
619-
wxc.backend_version)
620-
pass
621-
622-
# try to find the set size func across wx versions
623-
try:
624-
getattr(self, 'SetInitialSize')
625-
except AttributeError:
626-
self.SetInitialSize = getattr(self, 'SetBestFittingSize',
627-
do_nothing)
628-
629-
if not hasattr(self, 'IsShownOnScreen'):
630-
self.IsShownOnScreen = getattr(self, 'IsVisible',
631-
lambda *args: True)
632-
633634
# Create the drawing bitmap
634-
self.bitmap = wxc.EmptyBitmap(w, h)
635+
self.bitmap = wx.Bitmap(w, h)
635636
DEBUG_MSG("__init__() - bitmap w:%d h:%d" % (w, h), 2, self)
636637
# TODO: Add support for 'point' inspection and plot navigation.
637638
self._isDrawn = False
@@ -733,7 +734,7 @@ def start_event_loop(self, timeout=0):
733734
self.Bind(wx.EVT_TIMER, self.stop_event_loop, id=id)
734735

735736
# Event loop handler for start/stop event loop
736-
self._event_loop = wxc.EventLoop()
737+
self._event_loop = wx.GUIEventLoop()
737738
self._event_loop.Run()
738739
timer.Stop()
739740

@@ -845,7 +846,7 @@ def _onSize(self, evt):
845846
return
846847
self._width, self._height = size
847848
# Create a new, correctly sized bitmap
848-
self.bitmap = wxc.EmptyBitmap(self._width, self._height)
849+
self.bitmap = wx.Bitmap(self._width, self._height)
849850

850851
self._isDrawn = False
851852

@@ -1083,7 +1084,7 @@ def _print_image(self, filename, filetype, *args, **kwargs):
10831084
width = int(math.ceil(width))
10841085
height = int(math.ceil(height))
10851086

1086-
self.bitmap = wxc.EmptyBitmap(width, height)
1087+
self.bitmap = wx.Bitmap(width, height)
10871088

10881089
renderer = RendererWx(self.bitmap, self.figure.dpi)
10891090

@@ -1172,12 +1173,8 @@ def __init__(self, num, fig):
11721173
self.toolbar.Realize()
11731174
# On Windows platform, default window size is incorrect, so set
11741175
# toolbar width to figure width.
1175-
if wxc.is_phoenix:
1176-
tw, th = self.toolbar.GetSize()
1177-
fw, fh = self.canvas.GetSize()
1178-
else:
1179-
tw, th = self.toolbar.GetSizeTuple()
1180-
fw, fh = self.canvas.GetSizeTuple()
1176+
tw, th = self.toolbar.GetSize()
1177+
fw, fh = self.canvas.GetSize()
11811178
# By adding toolbar in sizer, we are able to put it at the bottom
11821179
# of the frame - so appearance is closer to GTK version.
11831180
self.toolbar.SetSize(wx.Size(fw, th))
@@ -1368,12 +1365,8 @@ def Destroy(self):
13681365

13691366
def _onMenuButton(self, evt):
13701367
"""Handle menu button pressed."""
1371-
if wxc.is_phoenix:
1372-
x, y = self.GetPosition()
1373-
w, h = self.GetSize()
1374-
else:
1375-
x, y = self.GetPositionTuple()
1376-
w, h = self.GetSizeTuple()
1368+
x, y = self.GetPosition()
1369+
w, h = self.GetSize()
13771370
self.PopupMenuXY(self._menu, x, y + h - 4)
13781371
# When menu returned, indicate selection in button
13791372
evt.Skip()
@@ -1500,11 +1493,15 @@ def _init_toolbar(self):
15001493
if text is None:
15011494
self.AddSeparator()
15021495
continue
1503-
self.wx_ids[text] = wx.NewId()
1504-
wxc._AddTool(self, self.wx_ids, text,
1505-
_load_bitmap(image_file + '.png'),
1506-
tooltip_text)
1507-
1496+
self.wx_ids[text] = (
1497+
self.AddTool(
1498+
-1,
1499+
bitmap=_load_bitmap(image_file + ".png"),
1500+
bmpDisabled=wx.NullBitmap,
1501+
label=text, shortHelp=text, longHelp=tooltip_text,
1502+
kind=(wx.ITEM_CHECK if text in ["Pan", "Zoom"]
1503+
else wx.ITEM_NORMAL))
1504+
.Id)
15081505
self.Bind(wx.EVT_TOOL, getattr(self, callback),
15091506
id=self.wx_ids[text])
15101507

@@ -1569,7 +1566,7 @@ def save_figure(self, *args):
15691566
error_msg_wx(str(e))
15701567

15711568
def set_cursor(self, cursor):
1572-
cursor = wxc.Cursor(cursord[cursor])
1569+
cursor = wx.Cursor(cursord[cursor])
15731570
self.canvas.SetCursor(cursor)
15741571
self.canvas.Update()
15751572

@@ -1645,17 +1642,14 @@ def draw_rubberband(self, event, x0, y0, x1, y1):
16451642
rubberBandColor = '#C0C0FF' # or load from config?
16461643

16471644
# Set a pen for the border
1648-
color = wxc.NamedColour(rubberBandColor)
1645+
color = wx.Colour(rubberBandColor)
16491646
dc.SetPen(wx.Pen(color, 1))
16501647

16511648
# use the same color, plus alpha for the brush
16521649
r, g, b, a = color.Get(True)
16531650
color.Set(r, g, b, 0x60)
16541651
dc.SetBrush(wx.Brush(color))
1655-
if wxc.is_phoenix:
1656-
dc.DrawRectangle(rect)
1657-
else:
1658-
dc.DrawRectangleRect(rect)
1652+
dc.DrawRectangle(rect)
16591653

16601654
def set_status_bar(self, statbar):
16611655
self.statbar = statbar
@@ -1742,7 +1736,7 @@ def trigger(self, *args):
17421736

17431737
class SetCursorWx(backend_tools.SetCursorBase):
17441738
def set_cursor(self, cursor):
1745-
cursor = wxc.Cursor(cursord[cursor])
1739+
cursor = wx.Cursor(cursord[cursor])
17461740
self.canvas.SetCursor(cursor)
17471741
self.canvas.Update()
17481742

@@ -1780,17 +1774,14 @@ def draw_rubberband(self, x0, y0, x1, y1):
17801774
rubberBandColor = '#C0C0FF' # or load from config?
17811775

17821776
# Set a pen for the border
1783-
color = wxc.NamedColour(rubberBandColor)
1777+
color = wx.Colour(rubberBandColor)
17841778
dc.SetPen(wx.Pen(color, 1))
17851779

17861780
# use the same color, plus alpha for the brush
17871781
r, g, b, a = color.Get(True)
17881782
color.Set(r, g, b, 0x60)
17891783
dc.SetBrush(wx.Brush(color))
1790-
if wxc.is_phoenix:
1791-
dc.DrawRectangle(rect)
1792-
else:
1793-
dc.DrawRectangleRect(rect)
1784+
dc.DrawRectangle(rect)
17941785

17951786
def remove_rubberband(self):
17961787
if self.wxoverlay is None:
@@ -1821,10 +1812,7 @@ def draw_rubberband(self, x0, y0, x1, y1):
18211812
dc.SetPen(wx.Pen(wx.BLACK, 1, wx.SOLID))
18221813
dc.SetBrush(wx.TRANSPARENT_BRUSH)
18231814
self._rect = (x0, self.canvas._height-y0, x1-x0, -y1+y0)
1824-
if wxc.is_phoenix:
1825-
dc.DrawRectangle(self._rect)
1826-
else:
1827-
dc.DrawRectangleRect(self._rect)
1815+
dc.DrawRectangle(self._rect)
18281816

18291817
def remove_rubberband(self, dc=None):
18301818
if not self._rect:
@@ -1872,13 +1860,10 @@ def OnPrintPage(self, page):
18721860
self.canvas.draw()
18731861

18741862
dc = self.GetDC()
1875-
(ppw, pph) = self.GetPPIPrinter() # printer's pixels per in
1876-
(pgw, pgh) = self.GetPageSizePixels() # page size in pixels
1877-
(dcw, dch) = dc.GetSize()
1878-
if wxc.is_phoenix:
1879-
(grw, grh) = self.canvas.GetSize()
1880-
else:
1881-
(grw, grh) = self.canvas.GetSizeTuple()
1863+
ppw, pph = self.GetPPIPrinter() # printer's pixels per in
1864+
pgw, pgh = self.GetPageSizePixels() # page size in pixels
1865+
dcw, dch = dc.GetSize()
1866+
grw, grh = self.canvas.GetSize()
18821867

18831868
# save current figure dpi resolution and bg color,
18841869
# so that we can temporarily set them to the dpi of

0 commit comments

Comments
 (0)