[Tutor] passing arguments to a make a graph
Alan Gauld
alan.gauld at btinternet.com
Fri Mar 23 21:04:12 CET 2007
"Che M" <pine508 at hotmail.com> wrote
> Thank you, Alan. I started by adding a method called addPoints()
> to my
> class as you recommend. But now I don't know how to pass a list of
> points
> to the addPoints() method.
You need to create an instance of a PlotPanel.
> class PlotPanel(wx.lib.plot.PlotCanvas):
> def __init__(self, points=[], *args, **kwargs):
> def addPoints(self, points):
> def _drawGraph(self):
> #this button tells it to make a graph using "mypoints".
>
> def OnGraphButton(self, event):
> mypoints = [(3,4), (5,6)] #these to be used
> PlotPanel.addPoints(PlotPanel,mypoints)
Instead of using the class create an instance(aka object)
self.plotPanel = PlotPanel(mypoints,self.notebook1)
That creates an instance passing the mypoints list in
to youir init method along with the notebook1 that you
pass when you create an instance later(see below).
> self.notebook1.AddPage(imageId=-1,
> page=PlotPanel(self.notebook1),
> select=True,
> text='Weight')
This should use the plotPanel instance:
self.notebook1.AddPage(imageId=-1, page=self.plotPanel,
select=True, text='Weight')
> The error I get using it this way is:
>
> TypeError: unbound method addPoints() must be called with PlotPanel
> instance as first argument (got type instance instead)
Yep, You gotta have an instance to call the methods.
See my OOP topic for more on classes, objects etc.
> I'm lost. Also, what does "self.points += points" mean? What is
> the +=
> operator?
x += 5
is shorthand for
x = x + 5
The same applies to -/* etc.
See my raw Materials topic for more info on operators.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list