8000 Merge pull request #10778 from anntzer/wxcnomore · matplotlib/matplotlib@8d54d60 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8d54d60

Browse files
Merge pull request #10778 from anntzer/wxcnomore
wx_compat is no more.
2 parents 1d73e73 + 5967c6f commit 8d54d60

File tree

6 files changed

+117
-230
lines changed

6 files changed

+117
-230
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, functions, and attributes are deprecated:
1012

examples/user_interfaces/mathtext_wx_sgskip.py

Lines changed: 8 additions & 5 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)
@@ -61,8 +60,9 @@ def __init__(self, parent, title):
6160

6261
# File Menu
6362
menu = wx.Menu()
64-
menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
63+
m_exit = menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
6564
menuBar.Append(menu, "&File")
65+
self.Bind(wx.EVT_MENU, self.OnClose, m_exit)
6666

6767
if IS_GTK or IS_WIN:
6868
# Equation Menu
@@ -71,7 +71,7 @@ def __init__(self, parent, title):
7171
bm = mathtext_to_wxbitmap(mt)
7272
item = wx.MenuItem(menu, 1000 + i, " ")
7373
item.SetBitmap(bm)
74-
menu.AppendItem(item)
74+
menu.Append(item)
7575
self.Bind(wx.EVT_MENU, self.OnChangePlot, item)
7676
menuBar.Append(menu, "&Functions")
7777

@@ -113,6 +113,9 @@ def change_plot(self, plot_number):
113113
self.axes.plot(t, s)
114114
self.canvas.draw()
115115

116+
def OnClose(self, event):
117+
self.Destroy()
118+
116119

117120
class MyApp(wx.App):
118121
def OnInit(self):

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 & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +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 (jeremy@o-donoghue.com)
6-
7-
Derived from original copyright work by John Hunter
8-
(jdhunter@ace.bsd.uchicago.edu)
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 (jeremy@o-donoghue.com) and John
5+
Hunter (jdhunter@ace.bsd.uchicago.edu).
146
7+
Copyright (C) Jeremy O'Donoghue & John Hunter, 2003-4.
158
"""
169

1710
import six
1811

19-
import sys
20-
import os
2112
import os.path
2213
import math
23-
import weakref
14+
import sys
2415
import warnings
16+
import weakref
2517

2618
import matplotlib
2719
from matplotlib.backend_bases import (
@@ -37,7 +29,6 @@
3729
from matplotlib.widgets import SubplotTool
3830
from matplotlib import cbook, rcParams, backend_tools
3931

40-
from . import wx_compat as wxc
4132
import wx
4233

4334
# Debugging settings here...
@@ -171,14 +162,45 @@ class RendererWx(RendererBase):
171162
# describes the colour and weight of any lines drawn, and a wxBrush
172163
# which describes the fill colour of any closed polygon.
173164

174-
fontweights = wxc.fontweights
175-
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+
}
176191

177-
# wxPython allows for portable font styles, choosing them appropriately
178-
# for the target platform. Map some standard font names to the portable
179-
# 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.
180194
# QUESTION: Is it be wise to agree standard fontnames across all backends?
181-
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+
}
182204

183205
def __init__(self, bitmap, dpi):
184206
"""
@@ -283,7 +305,7 @@ def draw_image(self, gc, x, y, im):
283305
w = self.width
284306
h = self.height
285307
rows, cols = im.shape[:2]
286-
bitmap = wxc.BitmapFromBuffer(cols, rows, im.tostring())
308+
bitmap = wx.Bitmap.FromBufferRGBA(cols, rows, im.tostring())
287309
gc = self.get_gc()
288310
gc.select()
289311
gc.gfx_ctx.DrawBitmap(bitmap, int(l), int(self.height - b),
@@ -610,7 +632,7 @@ def __init__(self, parent, id, figure):
610632
wx.Panel.__init__(self, parent, id, size=wx.Size(w, h))
611633

612634
# Create the drawing bitmap
613-
self.bitmap = wxc.EmptyBitmap(w, h)
635+
self.bitmap = wx.Bitmap(w, h)
614636
DEBUG_MSG("__init__() - bitmap w:%d h:%d" % (w, h), 2, self)
615637
# TODO: Add support for 'point' inspection and plot navigation.
616638
self._isDrawn = False
@@ -715,7 +737,7 @@ def start_event_loop(self, timeout=0):
715737
self.Bind(wx.EVT_TIMER, self.stop_event_loop, id=id)
716738

717739
# Event loop handler for start/stop event loop
718-
self._event_loop = wxc.EventLoop()
740+
self._event_loop = wx.GUIEventLoop()
719741
self._event_loop.Run()
720742
timer.Stop()
721743

@@ -829,7 +851,7 @@ def _onSize(self, evt):
829851
return
830852
self._width, self._height = size
831853
# Create a new, correctly sized bitmap
832-
self.bitmap = wxc.EmptyBitmap(self._width, self._height)
854+
self.bitmap = wx.Bitmap(self._width, self._height)
833855

834856
self._isDrawn = False
835857

@@ -1067,7 +1089,7 @@ def _print_image(self, filename, filetype, *args, **kwargs):
10671089
width = math.ceil(width)
10681090
height = math.ceil(height)
10691091

1070-
self.bitmap = wxc.EmptyBitmap(width, height)
1092+
self.bitmap = wx.Bitmap(width, height)
10711093

10721094
renderer = RendererWx(self.bitmap, self.figure.dpi)
10731095

@@ -1156,12 +1178,8 @@ def __init__(self, num, fig):
11561178
self.toolbar.Realize()
11571179
# On Windows platform, default window size is incorrect, so set
11581180
# toolbar width to figure width.
1159-
if wxc.is_phoenix:
1160-
tw, th = self.toolbar.GetSize()
1161-
fw, fh = self.canvas.GetSize()
1162-
else:
1163-
tw, th = self.toolbar.GetSizeTuple()
1164-
fw, fh = self.canvas.GetSizeTuple()
1181+
tw, th = self.toolbar.GetSize()
1182+
fw, fh = self.canvas.GetSize()
11651183
# By adding toolbar in sizer, we are able to put it at the bottom
11661184
# of the frame - so appearance is closer to GTK version.
11671185
self.toolbar.SetSize(wx.Size(fw, th))
@@ -1352,12 +1370,8 @@ def Destroy(self):
13521370

13531371
def _onMenuButton(self, evt):
13541372
"""Handle menu button pressed."""
1355-
if wxc.is_phoenix:
1356-
x, y = self.GetPosition()
1357-
w, h = self.GetSize()
1358-
else:
1359-
x, y = self.GetPositionTuple()
1360-
w, h = self.GetSizeTuple()
1373+
x, y = self.GetPosition()
1374+
w, h = self.GetSize()
13611375
self.PopupMenuXY(self._menu, x, y + h - 4)
13621376
# When menu returned, indicate selection in button
13631377
evt.Skip()
@@ -1484,11 +1498,15 @@ def _init_toolbar(self):
14841498
if text is None:
14851499
self.AddSeparator()
14861500
continue
1487-
self.wx_ids[text] = wx.NewId()
1488-
wxc._AddTool(self, self.wx_ids, text,
1489-
_load_bitmap(image_file + '.png'),
1490-
tooltip_text)
1491-
1501+
self.wx_ids[text] = (
1502+
self.AddTool(
1503+
-1,
1504+
bitmap=_load_bitmap(image_file + ".png"),
1505+
bmpDisabled=wx.NullBitmap,
1506+
label=text, shortHelp=text, longHelp=tooltip_text,
1507+
kind=(wx.ITEM_CHECK if text in ["Pan", "Zoom"]
1508+
else wx.ITEM_NORMAL))
1509+
.Id)
14921510
self.Bind(wx.EVT_TOOL, getattr(self, callback),
14931511
id=self.wx_ids[text])
14941512

@@ -1553,7 +1571,7 @@ def save_figure(self, *args):
15531571
error_msg_wx(str(e))
15541572

15551573
def set_cursor(self, cursor):
1556-
cursor = wxc.Cursor(cursord[cursor])
1574+
cursor = wx.Cursor(cursord[cursor])
15571575
self.canvas.SetCursor(cursor)
15581576
self.canvas.Update()
15591577

@@ -1629,17 +1647,14 @@ def draw_rubberband(self, event, x0, y0, x1, y1):
16291647
rubberBandColor = '#C0C0FF' # or load from config?
16301648

16311649
# Set a pen for the border
1632-
color = wxc.NamedColour(rubberBandColor)
1650+
color = wx.Colour(rubberBandColor)
16331651
dc.SetPen(wx.Pen(color, 1))
16341652

16351653
# use the same color, plus alpha for the brush
16361654
r, g, b, a = color.Get(True)
16371655
color.Set(r, g, b, 0x60)
16381656
dc.SetBrush(wx.Brush(color))
1639-
if wxc.is_phoenix:
1640-
dc.DrawRectangle(rect)
1641-
else:
1642-
dc.DrawRectangleRect(rect)
1657+
dc.DrawRectangle(rect)
16431658

16441659
def set_status_bar(self, statbar):
16451660
self.statbar = statbar
@@ -1726,7 +1741,7 @@ def trigger(self, *args):
17261741

17271742
class SetCursorWx(backend_tools.SetCursorBase):
17281743
def set_cursor(self, cursor):
1729-
cursor = wxc.Cursor(cursord[cursor])
1744+
cursor = wx.Cursor(cursord[cursor])
17301745
self.canvas.SetCursor(cursor)
17311746
self.canvas.Update()
17321747

@@ -1764,17 +1779,14 @@ def draw_rubberband(self, x0, y0, x1, y1):
17641779
rubberBandColor = '#C0C0FF' # or load from config?
17651780

17661781
# Set a pen for the border
1767-
color = wxc.NamedColour(rubberBandColor)
1782+
color = wx.Colour(rubberBandColor)
17681783
dc.SetPen(wx.Pen(color, 1))
17691784

17701785
# use the same color, plus alpha for the brush
17711786
r, g, b, a = color.Get(True)
17721787
color.Set(r, g, b, 0x60)
17731788
dc.SetBrush(wx.Brush(color))
1774-
if wxc.is_phoenix:
1775-
dc.DrawRectangle(rect)
1776-
else:
1777-
dc.DrawRectangleRect(rect)
1789+
dc.DrawRectangle(rect)
17781790

17791791
def remove_rubberband(self):
17801792
if self.wxoverlay is None:
@@ -1805,10 +1817,7 @@ def draw_rubberband(self, x0, y0, x1, y1):
18051817
dc.SetPen(wx.Pen(wx.BLACK, 1, wx.SOLID))
18061818
dc.SetBrush(wx.TRANSPARENT_BRUSH)
18071819
self._rect = (x0, self.canvas._height-y0, x1-x0, -y1+y0)
1808-
if wxc.is_phoenix:
1809-
dc.DrawRectangle(self._rect)
1810-
else:
1811-
dc.DrawRectangleRect(self._rect)
1820+
dc.DrawRectangle(self._rect)
18121821

18131822
def remove_rubberband(self, dc=None):
18141823
if not self._rect:
@@ -1856,13 +1865,10 @@ def OnPrintPage(self, page):
18561865
self.canvas.draw()
18571866

18581867
dc = self.GetDC()
1859-
(ppw, pph) = self.GetPPIPrinter() # printer's pixels per in
1860-
(pgw, pgh) = self.GetPageSizePixels() # page size in pixels
1861-
(dcw, dch) = dc.GetSize()
1862-
if wxc.is_phoenix:
1863-
(grw, grh) = self.canvas.GetSize()
1864-
else:
1865-
(grw, grh) = self.canvas.GetSizeTuple()
1868+
ppw, pph = self.GetPPIPrinter() # printer's pixels per in
1869+
pgw, pgh = self.GetPageSizePixels() # page size in pixels
1870+
dcw, dch = dc.GetSize()
1871+
grw, grh = self.canvas.GetSize()
18661872

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

0 commit comments

Comments
 (0)
0