I've just joined the site, and I'm embarassed to be posting what is obviously a simple problem, but I've had no luck with figuring it out. I'm sort of new to dealing with GUIs, wx, and much of the CLASS structure of Python. I've put together a simple script below to illustrate my problem. I have a long script that loads, and analyzes large data sets (originating from the scan of a microarray... if anybody is interested). After the analysis is complete, it then plots the results on a canvas. I've got a few problems here. I can't seem to work out the scope of the variables so that they can be seen in the drawline function, and I can't seem to get the function to draw on the canvas defined in the __init__ of the MyFrame class. Thanks again for any help or suggestions.


import wx 
from floatcanvas import NavCanvas, FloatCanvas

class MyFrame(wx.Frame): 
    """a frame with a panel"""
    def __init__(self, parent=None, id=-1, title=None): 
        wx.Frame.__init__(self, parent, id, title) 
        splitter = wx.SplitterWindow(self, style=wx.SP_LIVE_UPDATE|wx.SP_3D, size = (600,300))
        panel1 = wx.Panel(splitter)
    #Draw some widgets for controlling different variables on panel1.....
        panel2 = NavCanvas.NavCanvas(splitter,
                                     ProjectionFun = None,
                                     Debug = 0,
                                     BackgroundColor = "LIGHT GRAY",
        sash_Position = 100        
        splitter.SplitVertically(panel1, panel2, sash_Position)        
        min_Pan_size = 40
        panel2.Bind(wx.EVT_PAINT, self.drawline)
       # flat file is loaded and calculations are made.  Results are 'coordinates' (x0,y0, x1,y1....) to be plotted using drawline

    def drawline(self, event):
        # establish the painting surface
        dc = wx.PaintDC(self.panel2)
        dc.SetPen(wx.Pen('blue', 4))
        # draw line of coordinates calculated for each file
        dc.DrawLine(self.x0, self.y0, self.x1, self.y1)

#main program
app = wx.PySimpleApp() 
frame1 = MyFrame(title=microarray data) 

I can't seem to work out the scope of the variables so that they can be seen in the drawline function

The things in drawline that are referenced as being members of self are defined in __init__ without being a member of anything (ie they are objects that are created and subsequently destroyed in the scope of that function only). In order to make them members of self, define them as such!


# panel2 = NavCanvas.NavCanvas(...etc...) 
# Should be:
self.panel2 = NavCanvas.blahblahblah
        # x0=12
        # x1=23
        # y0=13
        # y1=26
# Should be:
        self.x0 = 12
        self.x1 = 23
        self.y0 = 13
        self.y1 = 26

I hope that clears it up.