Using an object inside a class

Jonno jonnojohnson at gmail.com
Mon Jan 23 16:23:06 EST 2012


Script...

import wx
import wx.aui
import matplotlib as mpl
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas

class Class1(wx.Panel):
    def __init__(self, parent, id = -1, dpi = None, **kwargs):
        wx.Panel.__init__(self, parent, id=id, **kwargs)
        self.figure = mpl.figure.Figure(dpi=dpi, figsize=(2,2))
        self.canvas = Canvas(self, -1, self.figure)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas,1,wx.EXPAND)
        test_button = wx.Button(self, wx.ID_ANY, 'Test')
        sizer.Add(test_button)
        self.SetSizer(sizer)
        self.Bind(wx.EVT_BUTTON, self.OnTest, id=wx.ID_ANY)

        # This doesn't work
        #app.frame.graph_panel.plot([1,2,3,4,5],[3,4,3,4,3])
        #app.frame.graph_panel.figure.canvas.draw()

    def OnTest(self, event):
        # This works
        app.frame.graph_panel.plot([1,2,3,4,5],[3,4,3,4,3])
        app.frame.graph_panel.figure.canvas.draw()


class Tab(wx.Panel):
    def __init__(self, parent, id = -1):
        wx.Panel.__init__(self, parent, id=id)
        self.nb = wx.aui.AuiNotebook(self)
        sizer = wx.BoxSizer()
        sizer.Add(self.nb, 1, wx.EXPAND)
        self.SetSizer(sizer)

    def add_axes(self,name="plot"):
       page = Class1(self.nb)
       self.nb.AddPage(page,name)
       return page.figure


class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
wx.Size(1000, 800))
        tab_panel = Tab(self)
        self.graph_panel = tab_panel.add_axes('Graph').gca()
        self.graph_panel.plot([1,2,3,4,5],[2,1,4,2,3])


class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, -1, 'App')
        self.frame.Show(True)
        return True

app = MyApp(0)
app.MainLoop()

On Mon, Jan 23, 2012 at 3:22 PM, Jonno <jonnojohnson at gmail.com> wrote:

>
>
> On Mon, Jan 23, 2012 at 2:25 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>
>> On 1/23/2012 2:44 PM, Jonno wrote:
>>
>>> I have a pretty complicated bit of code that I'm trying to convert to
>>> more clean OOP.
>>>
>>> Without getting too heavy into the details I have an object which I am
>>> trying to make available inside another class. The reference to the
>>> object is rather long and convoluted but what I find is that within my
>>> class definition this works:
>>>
>>> class Class1:
>>>     def __init__(self):
>>>
>>>     def method1(self):
>>>          foo.bar.object
>>>
>>> But this tells me "global name foo is not defined":
>>>
>>> class Class1:
>>>      def __init__(self):
>>>            foo.bar.object
>>>
>>> Obviously I want the object to be available throughout the class (I left
>>> out the self.object = etc for simplicity).
>>>
>>
>> Perhaps you left out some relevant details.
>>
>> I'm sure I did. Part of the reason I'm not posting the whole code is that
> I'm trying to teach myself OOP as part of this process. I want to figure
> out what is wrong as much as possible by myself. I really appreciate the
> pointers and suggestions though.
>
>
>>
>>  Any ideas why I can reference foo inside the method but not in __init__?
>>>
>>
>> References inside functions are resolved when the function is called. So
>> purely from what you have presented above, it would seem that 'foo' is
>> defined between the call to __init__ and a later call to method1.
>
>
> I have a strong suspicion that this is what's happening.
>
> Method1 is called on a button push when MainLoop is running so obviously
> foo (the main wx.App) exists by then.
> I must have somehow be initializing Class1 before foo = MyApp() happens.
> Is there a good reference on the order that things happen in python when a
> single script is run?
>
> In the meantime here is my stripped down script (foo = app, bar = frame,
> object = graph_panel). I'd welcome all suggestions to reorganize it.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120123/8424e55b/attachment-0001.html>


More information about the Python-list mailing list