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

Skip to content

Commit 758f826

Browse files
committed
Update wx backend to be compatible with wxPython classic 2.8.12+ on Py2.7+ and Phoenix Py2.7+ and 3.4+
Squashed the following commits: - set a variable at import time for wxPython classic/phoenix version - only use wxversion in Py2 - remove BeginDrawing/EndDrawing - remove wx.WXK_ PRIOR, NEXT, NUMPAD_PRIOR, NUMPAD_NEXT as deprecated for a long time - use wxversion only in Py2 - make it work on Py2.7 with wxPython 2.8.12, 2.9.5 and 3.0.2Phoenix - make it work on Py3.4 with 3.0.2Phoenix Conflicts: examples/user_interfaces/embedding_in_wx2.py - sometimes get an except: builtins.AttributeError: 'App' object has no attribute 'ProcessIdle' - use wxversion only in Py2 - make it work on Py2.7 with wxPython 2.8.12, 2.9.5 and 3.0.2Phoenix - make it work on Py3.4 with 3.0.2Phoenix Conflicts: examples/user_interfaces/fourier_demo_wx.py - revert incorrect change - use FileDialog styles which work for 2.8+ - move version specific stuff to wx_compat.py - PEP8 the code - pep8 lines to long - Skip is needed with wx backend and 2.8.12 to have Frame close - OnPaint event is handled by the backend, not needed here - Begin/EndDrawing is not needed - remove backend_wx and backend_wxagg from PEP8 from EXPECTED_BAD_FILES - clean up of examples, tested with wxPython 2.8.12.1, 3.0.1.1, Phoenix 3.0.2.dev77483 both for 'WX' and 'WXAgg' backends - remove special casing of toolbar for WXMAC, if necessary put it back in but check WXMAC version as it does not work in 10.9 - make 'WX' and 'WXAgg' work with wxPython 2.8, 2.9, 3.0 and 3.0.2 Phoenix - PEP8'ify - revert the EVT_PAINT handler - remove print lines - make use of wxversion optional - remove blank line after class added by autopep8 - fix blank after class and too excessive line split done by autopep8 - remove extraneous blank line - remove unneeded comma - some more autopep8 clean up - some more autopep8 clean up - m_keyCode is deprecated an no longer available as of 2.9 - pep8'ify to satisfy Travis CI - always get alpha, so same works in 2.8 and Phoenix - more descriptive name - adapt to wx_compat.py - fix deprecation for wx.StockCursor - statusBar.SetFieldsCount is not necessary and causes a two short field - add version select comment - add StockCursor rename - pep8 corrections use .Bind use .Bind, but keep AddSimpleTool as AddTool is a Phoenix addition improve comment remove redundant import clean up clean up minimal version check only in wx_compat.py rename method use distutils for version comp up wx version for Sphinx doc mocking clean up imports backend_version is now in wx_compat Oops, Toolbar is used in backends.__init__ fix PEP8 whitespace fix line splitting clean up
1 parent c85c67b commit 758f826

12 files changed

+722
-569
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):

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)