Skip to content

Update some wx examples #10416

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
Feb 12, 2018
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: 0 additions & 2 deletions examples/user_interfaces/embedding_in_wx2_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
toolbar - comment out the add_toolbar line for no toolbar
"""

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
Expand Down
1 change: 0 additions & 1 deletion examples/user_interfaces/embedding_in_wx3_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import os
import gc
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.cm as cm
import matplotlib.cbook as cbook
from matplotlib.backends.backend_wxagg import Toolbar, FigureCanvasWxAgg
Expand Down
8 changes: 0 additions & 8 deletions examples/user_interfaces/embedding_in_wx4_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
An example of how to use wx or wxagg in an application with a custom toolbar.
"""

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg
from matplotlib.backends.backend_wx import _load_bitmap
Expand Down Expand Up @@ -74,8 +72,6 @@ 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
self.Bind(wx.EVT_PAINT, self.OnPaint)

self.toolbar = MyNavigationToolbar(self.canvas, True)
self.toolbar.Realize()
Expand All @@ -88,10 +84,6 @@ def __init__(self):
self.SetSizer(self.sizer)
self.Fit()

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


class App(wx.App):
def OnInit(self):
Expand Down
56 changes: 24 additions & 32 deletions examples/user_interfaces/fourier_demo_wx_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import numpy as np

import wx
import matplotlib
matplotlib.interactive(False)
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure

Expand Down Expand Up @@ -108,55 +105,48 @@ def setKnob(self, value):
class FourierDemoFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
panel = wx.Panel(self)

self.fourierDemoWindow = FourierDemoWindow(self)
self.frequencySliderGroup = SliderGroup(
self,
label='Frequency f0:',
param=self.fourierDemoWindow.f0)
self.amplitudeSliderGroup = SliderGroup(self, label=' Amplitude a:',
param=self.fourierDemoWindow.A)
# create the GUI elements
self.createCanvas(panel)
self.createSliders(panel)

# place them in a sizer for the Layout
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.fourierDemoWindow, 1, wx.EXPAND)
sizer.Add(self.canvas, 1, wx.EXPAND)
sizer.Add(self.frequencySliderGroup.sizer, 0,
wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, border=5)
sizer.Add(self.amplitudeSliderGroup.sizer, 0,
wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, border=5)
self.SetSizer(sizer)

panel.SetSizer(sizer)

class FourierDemoWindow(wx.Window, Knob):
def __init__(self, *args, **kwargs):
wx.Window.__init__(self, *args, **kwargs)
def createCanvas(self, parent):
self.lines = []
self.figure = Figure()
self.canvas = FigureCanvasWxAgg(self, -1, self.figure)
self.canvas = FigureCanvasWxAgg(parent, -1, self.figure)
self.canvas.callbacks.connect('button_press_event', self.mouseDown)
self.canvas.callbacks.connect('motion_notify_event', self.mouseMotion)
self.canvas.callbacks.connect('button_release_event', self.mouseUp)
self.state = ''
self.mouseInfo = (None, None, None, None)
self.f0 = Param(2., minimum=0., maximum=6.)
self.A = Param(1., minimum=0.01, maximum=2.)
self.draw()
self.createPlots()

# Not sure I like having two params attached to the same Knob,
# but that is what we have here... it works but feels kludgy -
# although maybe it's not too bad since the knob changes both params
# at the same time (both f0 and A are affected during a drag)
self.f0.attach(self)
self.A.attach(self)
self.Bind(wx.EVT_SIZE, self.sizeHandler)

self.Bind(wx.EVT_PAINT, self.OnPaint)

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

def sizeHandler(self, *args, **kwargs):
self.canvas.SetSize(self.GetSize())
def createSliders(self, panel):
self.frequencySliderGroup = SliderGroup(
panel,
label='Frequency f0:',
param=self.f0)
self.amplitudeSliderGroup = SliderGroup(panel, label=' Amplitude a:',
param=self.A)

def mouseDown(self, evt):
if self.lines[0].contains(evt)[0]:
Expand Down Expand Up @@ -186,7 +176,10 @@ def mouseMotion(self, evt):
def mouseUp(self, evt):
self.state = ''

def draw(self):
def createPlots(self):
# This method creates the subplots, waveforms and labels.
# Later, when the waveforms or sliders are dragged, only the
# waveform data will be updated (not here, but below in setKnob).
if not hasattr(self, 'subplot1'):
self.subplot1, self.subplot2 = self.figure.subplots(2)
x1, y1, x2, y2 = self.compute(self.f0.value, self.A.value)
Expand Down Expand Up @@ -222,15 +215,14 @@ def compute(self, f0, A):
(np.exp(-np.pi * (f - f0) ** 2) + np.exp(-np.pi * (f + f0) ** 2))
return f, X, t, x

def repaint(self):
self.canvas.draw()

def setKnob(self, value):
# Note, we ignore value arg here and just go by state of the params
x1, y1, x2, y2 = self.compute(self.f0.value, self.A.value)
# update the data of the two waveforms
self.lines[0].set(xdata=x1, ydata=y1)
self.lines[1].set(xdata=x2, ydata=y2)
self.repaint()
# make the canvas draw its contents again with the new data
self.canvas.draw()


class App(wx.App):
Expand Down
2 changes: 0 additions & 2 deletions examples/user_interfaces/wxcursor_demo_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
Example to draw a cursor and report the data coords in wx.
"""

import matplotlib
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx, wxc
Expand Down