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

Skip to content

make wx backends compatible with wxPython-Phoenix #3421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class ToolBar(object):
class Frame(object):
pass

VERSION_STRING = '2.8'
VERSION_STRING = '2.8.12'


class MyPyQt4(MagicMock):
Expand Down
13 changes: 13 additions & 0 deletions doc/users/whats_new/updated_backend_wx.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
wx backend has been updated
---------------------------
The wx backend can now be used with both wxPython classic and
`Phoenix <http://wxpython.org/Phoenix/docs/html/main.html>`__.

wxPython classic has to be at least version 2.8.12 and works on Python 2.x. As
of May 2015 no official release of wxPython Phoenix is available but a
current snapshot will work on Python 2.7+ and 3.4+.

If you have multiple versions of wxPython installed, then the user code is
responsible setting the wxPython version. How to do this is
explained in the comment at the beginning of the example
`examples\user_interfaces\embedding_in_wx2.py`.
41 changes: 15 additions & 26 deletions examples/user_interfaces/embedding_in_wx2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
toolbar - comment out the setA_toolbar line for no toolbar
"""

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

from numpy import arange, sin, pi

Expand All @@ -25,25 +27,24 @@
from matplotlib.figure import Figure

import wx
import wx.lib.mixins.inspection as WIT


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

self.SetBackgroundColour(wx.NamedColour("WHITE"))

self.figure = Figure()
self.axes = self.figure.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
s = sin(2 * pi * t)

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

self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
self.SetSizer(self.sizer)
self.Fit()

Expand All @@ -52,31 +53,19 @@ def __init__(self):
def add_toolbar(self):
self.toolbar = NavigationToolbar2Wx(self.canvas)
self.toolbar.Realize()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing these checks, are you saying that the bug they work around are no longer in effect for wx v2.8 and above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 12/5/2014 16:40, Benjamin Root wrote:

In examples/user_interfaces/embedding_in_wx2.py:

@@ -52,31 +60,20 @@ def init(self):
def add_toolbar(self):
self.toolbar = NavigationToolbar2Wx(self.canvas)
self.toolbar.Realize()

  •    if wx.Platform == '**WXMAC**':
    

By removing these checks, are you saying that the bug they work around
are no longer in effect for wx v2.8 and above?

I don't have a MAC, I removed that based on the comment by jenshnielsen
on Aug 29 to this PR.

Werner

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will test this again, but yes this is no longer relevant.

if wx.Platform == '__WXMAC__':
# Mac platform (OSX 10.3, MacPython) does not seem to cope with
# having a toolbar in a sizer. This work-around gets the buttons
# back, but at the expense of having the toolbar at the top
self.SetToolBar(self.toolbar)
else:
# On Windows platform, default window size is incorrect, so set
# toolbar width to figure width.
tw, th = self.toolbar.GetSizeTuple()
fw, fh = self.canvas.GetSizeTuple()
# By adding toolbar in sizer, we are able to put it at the bottom
# of the frame - so appearance is closer to GTK version.
# As noted above, doesn't work for Mac.
self.toolbar.SetSize(wx.Size(fw, th))
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
# By adding toolbar in sizer, we are able to put it at the bottom
# of the frame - so appearance is closer to GTK version.
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
# update the axes menu on the toolbar
self.toolbar.update()

def OnPaint(self, event):
self.canvas.draw()


class App(wx.App):
# alternatively you could use
#class App(wx.App):
class App(WIT.InspectableApp):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new to me. Is this what wx users are supposed to use now? I want to make sure I get this right for my book that I am about to publish.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 12/5/2014 16:42, Benjamin Root wrote:

In examples/user_interfaces/embedding_in_wx2.py:

+class App(WIT.InspectableApp):

This is new to me. Is this what wx users are supposed to use now? I
want to make sure I get this right for my book that I am about to publish.

The WIT
(http://wxpython.org/Phoenix/docs/html/lib.mixins.inspection.html#module-lib.mixins.inspection)
allows debugging of the running application to e.g. figure out problems
with sizers and as it has a PyCrust pane which can also be very useful
in debugging, I use that setup in all my stuff.

so it should be the above or:

class App(wx.App):

but no longer use wx.PySimpleApp.

Werner

def OnInit(self):
'Create the main window and insert the custom frame'
self.Init()
frame = CanvasFrame()
frame.Show(True)

Expand Down
30 changes: 14 additions & 16 deletions examples/user_interfaces/embedding_in_wx3.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
"""
from __future__ import print_function

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

import sys
import time
Expand Down Expand Up @@ -54,7 +56,7 @@ def __init__(self, parent):
self.canvas = FigureCanvasWxAgg(self, -1, self.fig)
self.toolbar = Toolbar(self.canvas) # matplotlib toolbar
self.toolbar.Realize()
#self.toolbar.set_active([0,1])
# self.toolbar.set_active([0,1])

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

x = np.arange(120.0)*2*np.pi/60.0
y = np.arange(100.0)*2*np.pi/50.0
x = np.arange(120.0) * 2 * np.pi / 60.0
y = np.arange(100.0) * 2 * np.pi / 50.0
self.x, self.y = np.meshgrid(x, y)
z = np.sin(self.x) + np.cos(self.y)
self.im = a.imshow(z, cmap=cm.jet) # , interpolation='nearest')
Expand All @@ -88,8 +90,8 @@ def GetToolBar(self):
return self.toolbar

def OnWhiz(self, evt):
self.x += np.pi/15
self.y += np.pi/20
self.x += np.pi / 15
self.y += np.pi / 20
z = np.sin(self.x) + np.cos(self.y)
self.im.set_array(z)

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

class MyApp(wx.App):
def OnInit(self):
xrcfile = cbook.get_sample_data('embedding_in_wx3.xrc', asfileobj=False)
xrcfile = cbook.get_sample_data('embedding_in_wx3.xrc',
asfileobj=False)
print('loading', xrcfile)

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

# whiz button ------------------

whiz_button = xrc.XRCCTRL(self.frame, "whiz_button")
wx.EVT_BUTTON(whiz_button, whiz_button.GetId(),
self.plotpanel.OnWhiz)
whiz_button.Bind(wx.EVT_BUTTON, self.plotpanel.OnWhiz)

# bang button ------------------

bang_button = xrc.XRCCTRL(self.frame, "bang_button")
wx.EVT_BUTTON(bang_button, bang_button.GetId(),
self.OnBang)
bang_button.Bind(wx.EVT_BUTTON, self.OnBang)

# final setup ------------------

sizer = self.panel.GetSizer()
self.frame.Show(1)

Expand Down
44 changes: 19 additions & 25 deletions examples/user_interfaces/embedding_in_wx4.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
toolbar
"""

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

from numpy import arange, sin, pi

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

# for simplicity I'm going to reuse a bitmap from wx, you'll
# probably want to add your own.
self.AddSimpleTool(self.ON_CUSTOM, _load_bitmap('stock_left.xpm'),
'Click me', 'Activate custom contol')
wx.EVT_TOOL(self, self.ON_CUSTOM, self._on_custom)
if 'phoenix' in wx.PlatformInfo:
self.AddTool(self.ON_CUSTOM, 'Click me',
_load_bitmap('stock_left.xpm'),
'Activate custom contol')
self.Bind(wx.EVT_TOOL, self._on_custom, id=self.ON_CUSTOM)
else:
self.AddSimpleTool(self.ON_CUSTOM, _load_bitmap('stock_left.xpm'),
'Click me', 'Activate custom contol')
self.Bind(wx.EVT_TOOL, self._on_custom, id=self.ON_CUSTOM)

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

self.SetBackgroundColour(wx.NamedColour("WHITE"))

self.figure = Figure(figsize=(5, 4), dpi=100)
self.axes = self.figure.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
s = sin(2 * pi * t)

self.axes.plot(t, s)

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

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

# update the axes menu on the toolbar
self.toolbar.update()
Expand Down
28 changes: 20 additions & 8 deletions examples/user_interfaces/embedding_in_wx5.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# Used to guarantee to use at least Wx2.8
import wxversion
wxversion.ensureMinimal('2.8')
#!/usr/bin/env python

# matplotlib requires wxPython 2.8+
# set the wxPython version in lib\site-packages\wx.pth file
# or if you have wxversion installed un-comment the lines below
#import wxversion
#wxversion.ensureMinimal('2.8')

import wx
import wx.aui
import wx.lib.mixins.inspection as wit

if 'phoenix' in wx.PlatformInfo:
import wx.lib.agw.aui as aui
else:
import wx.aui as aui

import matplotlib as mpl
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2Wx as Toolbar
Expand All @@ -26,7 +36,7 @@ def __init__(self, parent, id=-1, dpi=None, **kwargs):
class PlotNotebook(wx.Panel):
def __init__(self, parent, id=-1):
wx.Panel.__init__(self, parent, id=id)
self.nb = wx.aui.AuiNotebook(self)
self.nb = aui.AuiNotebook(self)
sizer = wx.BoxSizer()
sizer.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(sizer)
Expand All @@ -38,15 +48,17 @@ def add(self, name="plot"):


def demo():
app = wx.PySimpleApp()
# alternatively you could use
#app = wx.App()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment might need more explanation. As it stands, the user is left wondering what the difference is.

# InspectableApp is a great debug tool, see:
# http://wiki.wxpython.org/Widget%20Inspection%20Tool
app = wit.InspectableApp()
frame = wx.Frame(None, -1, 'Plotter')
plotter = PlotNotebook(frame)
axes1 = plotter.add('figure 1').gca()
axes1.plot([1, 2, 3], [2, 1, 4])
axes2 = plotter.add('figure 2').gca()
axes2.plot([1, 2, 3, 4, 5], [2, 1, 4, 2, 3])
#axes1.figure.canvas.draw()
#axes2.figure.canvas.draw()
frame.Show()
app.MainLoop()

Expand Down
Loading