@@ -108,55 +108,48 @@ def setKnob(self, value):
108108class FourierDemoFrame (wx .Frame ):
109109 def __init__ (self , * args , ** kwargs ):
110110 wx .Frame .__init__ (self , * args , ** kwargs )
111+ panel = wx .Panel (self )
111112
112- self .fourierDemoWindow = FourierDemoWindow (self )
113- self .frequencySliderGroup = SliderGroup (
114- self ,
115- label = 'Frequency f0:' ,
116- param = self .fourierDemoWindow .f0 )
117- self .amplitudeSliderGroup = SliderGroup (self , label = ' Amplitude a:' ,
118- param = self .fourierDemoWindow .A )
113+ # create the GUI elements
114+ self .createCanvas (panel )
115+ self .createSliders (panel )
119116
117+ # place them in a sizer for the Layout
120118 sizer = wx .BoxSizer (wx .VERTICAL )
121- sizer .Add (self .fourierDemoWindow , 1 , wx .EXPAND )
119+ sizer .Add (self .canvas , 1 , wx .EXPAND )
122120 sizer .Add (self .frequencySliderGroup .sizer , 0 ,
123121 wx .EXPAND | wx .ALIGN_CENTER | wx .ALL , border = 5 )
124122 sizer .Add (self .amplitudeSliderGroup .sizer , 0 ,
125123 wx .EXPAND | wx .ALIGN_CENTER | wx .ALL , border = 5 )
126- self .SetSizer (sizer )
127-
124+ panel .SetSizer (sizer )
128125
129- class FourierDemoWindow (wx .Window , Knob ):
130- def __init__ (self , * args , ** kwargs ):
131- wx .Window .__init__ (self , * args , ** kwargs )
126+ def createCanvas (self , parent ):
132127 self .lines = []
133128 self .figure = Figure ()
134- self .canvas = FigureCanvasWxAgg (self , - 1 , self .figure )
129+ self .canvas = FigureCanvasWxAgg (parent , - 1 , self .figure )
135130 self .canvas .callbacks .connect ('button_press_event' , self .mouseDown )
136131 self .canvas .callbacks .connect ('motion_notify_event' , self .mouseMotion )
137132 self .canvas .callbacks .connect ('button_release_event' , self .mouseUp )
138133 self .state = ''
139134 self .mouseInfo = (None , None , None , None )
140135 self .f0 = Param (2. , minimum = 0. , maximum = 6. )
141136 self .A = Param (1. , minimum = 0.01 , maximum = 2. )
142- self .draw ()
137+ self .createPlots ()
143138
144139 # Not sure I like having two params attached to the same Knob,
145140 # but that is what we have here... it works but feels kludgy -
146141 # although maybe it's not too bad since the knob changes both params
147142 # at the same time (both f0 and A are affected during a drag)
148143 self .f0 .attach (self )
149144 self .A .attach (self )
150- self .Bind (wx .EVT_SIZE , self .sizeHandler )
151-
152- self .Bind (wx .EVT_PAINT , self .OnPaint )
153-
154- def OnPaint (self , event ):
155- self .canvas .draw ()
156- event .Skip ()
157145
158- def sizeHandler (self , * args , ** kwargs ):
159- self .canvas .SetSize (self .GetSize ())
146+ def createSliders (self , panel ):
147+ self .frequencySliderGroup = SliderGroup (
148+ panel ,
149+ label = 'Frequency f0:' ,
150+ param = self .f0 )
151+ self .amplitudeSliderGroup = SliderGroup (panel , label = ' Amplitude a:' ,
152+ param = self .A )
160153
161154 def mouseDown (self , evt ):
162155 if self .lines [0 ].contains (evt )[0 ]:
@@ -186,7 +179,10 @@ def mouseMotion(self, evt):
186179 def mouseUp (self , evt ):
187180 self .state = ''
188181
189- def draw (self ):
182+ def createPlots (self ):
183+ # this method creates subplots, adds labels etc.
184+ # later, when the waveforms or sliders are dragged,
185+ # only the data will be updated
190186 if not hasattr (self , 'subplot1' ):
191187 self .subplot1 , self .subplot2 = self .figure .subplots (2 )
192188 x1 , y1 , x2 , y2 = self .compute (self .f0 .value , self .A .value )
@@ -222,15 +218,14 @@ def compute(self, f0, A):
222218 (np .exp (- np .pi * (f - f0 ) ** 2 ) + np .exp (- np .pi * (f + f0 ) ** 2 ))
223219 return f , X , t , x
224220
225- def repaint (self ):
226- self .canvas .draw ()
227-
228221 def setKnob (self , value ):
229222 # Note, we ignore value arg here and just go by state of the params
230223 x1 , y1 , x2 , y2 = self .compute (self .f0 .value , self .A .value )
224+ # update the data of the two waveforms
231225 self .lines [0 ].set (xdata = x1 , ydata = y1 )
232226 self .lines [1 ].set (xdata = x2 , ydata = y2 )
233- self .repaint ()
227+ # make the canvas draw its contents again with the new data
228+ self .canvas .draw ()
234229
235230
236231class App (wx .App ):
0 commit comments