Skip to content

Commit 128e99a

Browse files
committed
Merge pull request matplotlib#3421 from wernerfb/wxPythonPhoenixAug2014
make wx backends compatible with wxPython-Phoenix
2 parents f3bf9a8 + 12cf540 commit 128e99a

13 files changed

+736
-567
lines changed

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class ToolBar(object):
295295
class Frame(object):
296296
pass
297297

298-
VERSION_STRING = '2.8'
298+
VERSION_STRING = '2.8.12'
299299

300300

301301
class MyPyQt4(MagicMock):
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
wx backend has been updated
2+
---------------------------
3+
The wx backend can now be used with both wxPython classic and
4+
`Phoenix <http://wxpython.org/Phoenix/docs/html/main.html>`__.
5+
6+
wxPython classic has to be at least version 2.8.12 and works on Python 2.x. As
7+
of May 2015 no official release of wxPython Phoenix is available but a
8+
current snapshot will work on Python 2.7+ and 3.4+.
9+
10+
If you have multiple versions of wxPython installed, then the user code is
11+
responsible setting the wxPython version. How to do this is
12+
explained in the comment at the beginning of the example
13+
`examples\user_interfaces\embedding_in_wx2.py`.

examples/user_interfaces/embedding_in_wx2.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
toolbar - comment out the setA_toolbar line for no toolbar
55
"""
66

7-
# Used to guarantee to use at least Wx2.8
8-
import wxversion
9-
wxversion.ensureMinimal('2.8')
7+
# matplotlib requires wxPython 2.8+
8+
# set the wxPython version in lib\site-packages\wx.pth file
9+
# or if you have wxversion installed un-comment the lines below
10+
#import wxversion
11+
#wxversion.ensureMinimal('2.8')
1012

1113
from numpy import arange, sin, pi
1214

@@ -25,25 +27,24 @@
2527
from matplotlib.figure import Figure
2628

2729
import wx
30+
import wx.lib.mixins.inspection as WIT
2831

2932

3033
class CanvasFrame(wx.Frame):
3134
def __init__(self):
3235
wx.Frame.__init__(self, None, -1,
3336
'CanvasFrame', size=(550, 350))
3437

35-
self.SetBackgroundColour(wx.NamedColour("WHITE"))
36-
3738
self.figure = Figure()
3839
self.axes = self.figure.add_subplot(111)
3940
t = arange(0.0, 3.0, 0.01)
40-
s = sin(2*pi*t)
41+
s = sin(2 * pi * t)
4142

4243
self.axes.plot(t, s)
4344
self.canvas = FigureCanvas(self, -1, self.figure)
4445

4546
self.sizer = wx.BoxSizer(wx.VERTICAL)
46-
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
47+
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
4748
self.SetSizer(self.sizer)
4849
self.Fit()
4950

@@ -52,31 +53,19 @@ def __init__(self):
5253
def add_toolbar(self):
5354
self.toolbar = NavigationToolbar2Wx(self.canvas)
5455
self.toolbar.Realize()
55-
if wx.Platform == '__WXMAC__':
56-
# Mac platform (OSX 10.3, MacPython) does not seem to cope with
57-
# having a toolbar in a sizer. This work-around gets the buttons
58-
# back, but at the expense of having the toolbar at the top
59-
self.SetToolBar(self.toolbar)
60-
else:
61-
# On Windows platform, default window size is incorrect, so set
62-
# toolbar width to figure width.
63-
tw, th = self.toolbar.GetSizeTuple()
64-
fw, fh = self.canvas.GetSizeTuple()
65-
# By adding toolbar in sizer, we are able to put it at the bottom
66-
# of the frame - so appearance is closer to GTK version.
67-
# As noted above, doesn't work for Mac.
68-
self.toolbar.SetSize(wx.Size(fw, th))
69-
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
56+
# By adding toolbar in sizer, we are able to put it at the bottom
57+
# of the frame - so appearance is closer to GTK version.
58+
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
7059
# update the axes menu on the toolbar
7160
self.toolbar.update()
7261

73-
def OnPaint(self, event):
74-
self.canvas.draw()
75-
7662

77-
class App(wx.App):
63+
# alternatively you could use
64+
#class App(wx.App):
65+
class App(WIT.InspectableApp):
7866
def OnInit(self):
7967
'Create the main window and insert the custom frame'
68+
self.Init()
8069
frame = CanvasFrame()
8170
frame.Show(True)
8271

examples/user_interfaces/embedding_in_wx3.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
"""
2222
from __future__ import print_function
2323

24-
# Used to guarantee to use at least Wx2.8
25-
import wxversion
26-
wxversion.ensureMinimal('2.8')
24+
# matplotlib requires wxPython 2.8+
25+
# set the wxPython version in lib\site-packages\wx.pth file
26+
# or if you have wxversion installed un-comment the lines below
27+
#import wxversion
28+
#wxversion.ensureMinimal('2.8')
2729

2830
import sys
2931
import time
@@ -54,7 +56,7 @@ def __init__(self, parent):
5456
self.canvas = FigureCanvasWxAgg(self, -1, self.fig)
5557
self.toolbar = Toolbar(self.canvas) # matplotlib toolbar
5658
self.toolbar.Realize()
57-
#self.toolbar.set_active([0,1])
59+
# self.toolbar.set_active([0,1])
5860

5961
# Now put all into a sizer
6062
sizer = wx.BoxSizer(wx.VERTICAL)
@@ -68,8 +70,8 @@ def __init__(self, parent):
6870
def init_plot_data(self):
6971
a = self.fig.add_subplot(111)
7072

71-
x = np.arange(120.0)*2*np.pi/60.0
72-
y = np.arange(100.0)*2*np.pi/50.0
73+
x = np.arange(120.0) * 2 * np.pi / 60.0
74+
y = np.arange(100.0) * 2 * np.pi / 50.0
7375
self.x, self.y = np.meshgrid(x, y)
7476
z = np.sin(self.x) + np.cos(self.y)
7577
self.im = a.imshow(z, cmap=cm.jet) # , interpolation='nearest')
@@ -88,8 +90,8 @@ def GetToolBar(self):
8890
return self.toolbar
8991

9092
def OnWhiz(self, evt):
91-
self.x += np.pi/15
92-
self.y += np.pi/20
93+
self.x += np.pi / 15
94+
self.y += np.pi / 20
9395
z = np.sin(self.x) + np.cos(self.y)
9496
self.im.set_array(z)
9597

@@ -108,7 +110,8 @@ def onEraseBackground(self, evt):
108110

109111
class MyApp(wx.App):
110112
def OnInit(self):
111-
xrcfile = cbook.get_sample_data('embedding_in_wx3.xrc', asfileobj=False)
113+
xrcfile = cbook.get_sample_data('embedding_in_wx3.xrc',
114+
asfileobj=False)
112115
print('loading', xrcfile)
113116

114117
self.res = xrc.XmlResource(xrcfile)
@@ -134,19 +137,14 @@ def OnInit(self):
134137
plot_container.SetSizer(sizer)
135138

136139
# whiz button ------------------
137-
138140
whiz_button = xrc.XRCCTRL(self.frame, "whiz_button")
139-
wx.EVT_BUTTON(whiz_button, whiz_button.GetId(),
140-
self.plotpanel.OnWhiz)
141+
whiz_button.Bind(wx.EVT_BUTTON, self.plotpanel.OnWhiz)
141142

142143
# bang button ------------------
143-
144144
bang_button = xrc.XRCCTRL(self.frame, "bang_button")
145-
wx.EVT_BUTTON(bang_button, bang_button.GetId(),
146-
self.OnBang)
145+
bang_button.Bind(wx.EVT_BUTTON, self.OnBang)
147146

148147
# final setup ------------------
149-
150148
sizer = self.panel.GetSizer()
151149
self.frame.Show(1)
152150

examples/user_interfaces/embedding_in_wx4.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
toolbar
55
"""
66

7-
# Used to guarantee to use at least Wx2.8
8-
import wxversion
9-
wxversion.ensureMinimal('2.8')
7+
# matplotlib requires wxPython 2.8+
8+
# set the wxPython version in lib\site-packages\wx.pth file
9+
# or if you have wxversion installed un-comment the lines below
10+
#import wxversion
11+
#wxversion.ensureMinimal('2.8')
1012

1113
from numpy import arange, sin, pi
1214

@@ -34,9 +36,15 @@ def __init__(self, canvas, cankill):
3436

3537
# for simplicity I'm going to reuse a bitmap from wx, you'll
3638
# probably want to add your own.
37-
self.AddSimpleTool(self.ON_CUSTOM, _load_bitmap('stock_left.xpm'),
38-
'Click me', 'Activate custom contol')
39-
wx.EVT_TOOL(self, self.ON_CUSTOM, self._on_custom)
39+
if 'phoenix' in wx.PlatformInfo:
40+
self.AddTool(self.ON_CUSTOM, 'Click me',
41+
_load_bitmap('stock_left.xpm'),
42+
'Activate custom contol')
43+
self.Bind(wx.EVT_TOOL, self._on_custom, id=self.ON_CUSTOM)
44+
else:
45+
self.AddSimpleTool(self.ON_CUSTOM, _load_bitmap('stock_left.xpm'),
46+
'Click me', 'Activate custom contol')
47+
self.Bind(wx.EVT_TOOL, self._on_custom, id=self.ON_CUSTOM)
4048

4149
def _on_custom(self, evt):
4250
# add some text to the axes in a random location in axes (0,1)
@@ -62,12 +70,10 @@ def __init__(self):
6270
wx.Frame.__init__(self, None, -1,
6371
'CanvasFrame', size=(550, 350))
6472

65-
self.SetBackgroundColour(wx.NamedColour("WHITE"))
66-
6773
self.figure = Figure(figsize=(5, 4), dpi=100)
6874
self.axes = self.figure.add_subplot(111)
6975
t = arange(0.0, 3.0, 0.01)
70-
s = sin(2*pi*t)
76+
s = sin(2 * pi * t)
7177

7278
self.axes.plot(t, s)
7379

@@ -76,25 +82,13 @@ def __init__(self):
7682
self.sizer = wx.BoxSizer(wx.VERTICAL)
7783
self.sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
7884
# Capture the paint message
79-
wx.EVT_PAINT(self, self.OnPaint)
85+
self.Bind(wx.EVT_PAINT, self.OnPaint)
8086

8187
self.toolbar = MyNavigationToolbar(self.canvas, True)
8288
self.toolbar.Realize()
83-
if wx.Platform == '__WXMAC__':
84-
# Mac platform (OSX 10.3, MacPython) does not seem to cope with
85-
# having a toolbar in a sizer. This work-around gets the buttons
86-
# back, but at the expense of having the toolbar at the top
87-
self.SetToolBar(self.toolbar)
88-
else:
89-
# On Windows platform, default window size is incorrect, so set
90-
# toolbar width to figure width.
91-
tw, th = self.toolbar.GetSizeTuple()
92-
fw, fh = self.canvas.GetSizeTuple()
93-
# By adding toolbar in sizer, we are able to put it at the bottom
94-
# of the frame - so appearance is closer to GTK version.
95-
# As noted above, doesn't work for Mac.
96-
self.toolbar.SetSize(wx.Size(fw, th))
97-
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
89+
# By adding toolbar in sizer, we are able to put it at the bottom
90+
# of the frame - so appearance is closer to GTK version.
91+
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
9892

9993
# update the axes menu on the toolbar
10094
self.toolbar.update()

examples/user_interfaces/embedding_in_wx5.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
# Used to guarantee to use at least Wx2.8
2-
import wxversion
3-
wxversion.ensureMinimal('2.8')
1+
#!/usr/bin/env python
2+
3+
# matplotlib requires wxPython 2.8+
4+
# set the wxPython version in lib\site-packages\wx.pth file
5+
# or if you have wxversion installed un-comment the lines below
6+
#import wxversion
7+
#wxversion.ensureMinimal('2.8')
48

59
import wx
6-
import wx.aui
10+
import wx.lib.mixins.inspection as wit
11+
12+
if 'phoenix' in wx.PlatformInfo:
13+
import wx.lib.agw.aui as aui
14+
else:
15+
import wx.aui as aui
16+
717
import matplotlib as mpl
818
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas
919
from matplotlib.backends.backend_wxagg import NavigationToolbar2Wx as Toolbar
@@ -26,7 +36,7 @@ def __init__(self, parent, id=-1, dpi=None, **kwargs):
2636
class PlotNotebook(wx.Panel):
2737
def __init__(self, parent, id=-1):
2838
wx.Panel.__init__(self, parent, id=id)
29-
self.nb = wx.aui.AuiNotebook(self)
39+
self.nb = aui.AuiNotebook(self)
3040
sizer = wx.BoxSizer()
3141
sizer.Add(self.nb, 1, wx.EXPAND)
3242
self.SetSizer(sizer)
@@ -38,15 +48,17 @@ def add(self, name="plot"):
3848

3949

4050
def demo():
41-
app = wx.PySimpleApp()
51+
# alternatively you could use
52+
#app = wx.App()
53+
# InspectableApp is a great debug tool, see:
54+
# http://wiki.wxpython.org/Widget%20Inspection%20Tool
55+
app = wit.InspectableApp()
4256
frame = wx.Frame(None, -1, 'Plotter')
4357
plotter = PlotNotebook(frame)
4458
axes1 = plotter.add('figure 1').gca()
4559
axes1.plot([1, 2, 3], [2, 1, 4])
4660
axes2 = plotter.add('figure 2').gca()
4761
axes2.plot([1, 2, 3, 4, 5], [2, 1, 4, 2, 3])
48-
#axes1.figure.canvas.draw()
49-
#axes2.figure.canvas.draw()
5062
frame.Show()
5163
app.MainLoop()
5264

0 commit comments

Comments
 (0)