[Tutor] passing arguments via an instance of a class

Che M pine508 at hotmail.com
Wed Apr 4 07:14:39 CEST 2007

Hi.  Alan G helped me a week or two back with how to pass a list of points 
to a class which could use it to draw a graph..  Now I am doing it again, 
but with different code (based on an example of embedding a matplotlib plot 
in wxPython) and having trouble.

That same basic issue is:  I need to create an instance of a class and in so 
doing pass the class two lists of numbers to serve as points in the graph.  
However, I can't get it to work because in the various ways I've tried it, 
the class is either not expecting to be passed a list as an argument, or it 
is not providing the list to its draw() method so nothing is plotted.

My problem is I don't understand how to create the class such that it a) 
expects to be passed two lists, and b) hands those lists off to its draw() 
method to use in drawing the graph.  Relevant code and some errors I got 
provided below.

class PlotPanel(wx.Panel):

    def __init__(self,parent, xpoints=[], ypoints=[], id = -1, color = 
        dpi = None, style = wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs):
        wx.Panel.__init__(self, parent, id = id, style = style, **kwargs)
        self.figure = Figure(None, dpi)
        self.xpoints = xpoints
        self.ypoints = ypoints
       #the NoRepaintCanvas class code snipped out
        self.canvas = NoRepaintCanvas(self, -1, self.figure)
        self.Bind(wx.EVT_IDLE, self._onIdle)
        self.Bind(wx.EVT_SIZE, self._onSize)
        self._resizeflag = True

# ... various code snipped which determines plot, size, color, etc...

#This draw method is what does the work of drawing the points.

    def draw(self):
        if not hasattr(self, 'subplot'):
            self.subplot = self.figure.add_subplot(111)

#if the following two lines were not commented out, the graph works fine
#but the point is to have these passed in by a user choice, not just
#always these same points.

        #x = [1,2,3]
        #y = [5,10,15]

#this next just plots a red line using whatever x,y points given

        self.subplot.plot(x,y, '-r')

#This is now the button which is supposed to feed the PlotPanel
#the points it needs.  It is a method of the wxFrame (code not shown)

def OnButton1Button(self, event):
#these are the points I want to be plotted when I push this button
        self.weightplot = PlotPanel(self.notebook1,xpoints,ypoints)
        self.notebook1.AddPage(imageId=-1, page=self.weightplot,
                select=True, text='Test Plot')


As is, when I press that button I get this error:

Name Error:  global name xpoints is not defined.

Suggesting the draw() method never got passed the xpoints (or the ypoints) 
via the button press.
I think I'm not setting up either the PlotPanel's __init__ or the draw() 
method or something to be able to receive these x and y lists, but I've 
tried it a number of different ways and can't figure it out.

Any advice is appreciated.

MSN is giving away a trip to Vegas to see Elton John.  Enter to win today. 

More information about the Tutor mailing list