Script...<div><br></div><div><div>import wx</div><div>import wx.aui</div><div>import matplotlib as mpl</div><div>from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas</div><div><br></div><div>class Class1(wx.Panel):</div>
<div> def __init__(self, parent, id = -1, dpi = None, **kwargs):</div><div> wx.Panel.__init__(self, parent, id=id, **kwargs)</div><div> self.figure = mpl.figure.Figure(dpi=dpi, figsize=(2,2))</div><div> self.canvas = Canvas(self, -1, self.figure)</div>
<div> sizer = wx.BoxSizer(wx.VERTICAL)</div><div> sizer.Add(self.canvas,1,wx.EXPAND)</div><div> test_button = wx.Button(self, wx.ID_ANY, 'Test')</div><div> sizer.Add(test_button)</div><div>
self.SetSizer(sizer)</div><div> self.Bind(wx.EVT_BUTTON, self.OnTest, id=wx.ID_ANY)</div><div> </div><div> # This doesn't work</div><div> #app.frame.graph_panel.plot([1,2,3,4,5],[3,4,3,4,3])</div>
<div> #app.frame.graph_panel.figure.canvas.draw()</div><div><br></div><div> def OnTest(self, event):</div><div> # This works</div><div> app.frame.graph_panel.plot([1,2,3,4,5],[3,4,3,4,3])</div><div>
app.frame.graph_panel.figure.canvas.draw()</div><div><br></div><div><br></div><div>class Tab(wx.Panel):</div><div> def __init__(self, parent, id = -1):</div><div> wx.Panel.__init__(self, parent, id=id)</div>
<div> self.nb = wx.aui.AuiNotebook(self)</div><div> sizer = wx.BoxSizer()</div><div> sizer.Add(self.nb, 1, wx.EXPAND)</div><div> self.SetSizer(sizer)</div><div><br></div><div> def add_axes(self,name="plot"):</div>
<div> page = Class1(self.nb)</div><div> self.nb.AddPage(page,name)</div><div> return page.figure</div><div> </div><div><br></div><div>class MyFrame(wx.Frame):</div><div> def __init__(self, parent, id, title):</div>
<div> wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(1000, 800)) </div><div> tab_panel = Tab(self)</div><div> self.graph_panel = tab_panel.add_axes('Graph').gca()</div>
<div> self.graph_panel.plot([1,2,3,4,5],[2,1,4,2,3])</div><div><br></div><div> </div><div>class MyApp(wx.App):</div><div> def OnInit(self):</div><div> self.frame = MyFrame(None, -1, 'App')</div>
<div> self.frame.Show(True)</div><div> return True</div><div> </div><div>app = MyApp(0)</div><div>app.MainLoop()</div><br><div class="gmail_quote">On Mon, Jan 23, 2012 at 3:22 PM, Jonno <span dir="ltr"><<a href="mailto:jonnojohnson@gmail.com">jonnojohnson@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br><br><div class="gmail_quote"><div class="im">On Mon, Jan 23, 2012 at 2:25 PM, Terry Reedy <span dir="ltr"><<a href="mailto:tjreedy@udel.edu" target="_blank">tjreedy@udel.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div>On 1/23/2012 2:44 PM, Jonno wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have a pretty complicated bit of code that I'm trying to convert to<br>
more clean OOP.<br>
<br>
Without getting too heavy into the details I have an object which I am<br>
trying to make available inside another class. The reference to the<br>
object is rather long and convoluted but what I find is that within my<br>
class definition this works:<br>
<br>
class Class1:<br>
def __init__(self):<br>
<br>
def method1(self):<br>
foo.bar.object<br>
<br>
But this tells me "global name foo is not defined":<br>
<br>
class Class1:<br>
def __init__(self):<br>
foo.bar.object<br>
<br>
Obviously I want the object to be available throughout the class (I left<br>
out the self.object = etc for simplicity).<br>
</blockquote>
<br></div></div>
Perhaps you left out some relevant details.<div><br></div></blockquote></div><div>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.</div>
<div class="im">
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Any ideas why I can reference foo inside the method but not in __init__?<br>
</blockquote>
<br></div>
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.</blockquote>
<div><br></div></div><div>I have a strong suspicion that this is what's happening.</div><div><br></div><div>Method1 is called on a button push when MainLoop is running so obviously foo (the main wx.App) exists by then. </div>
<div>I must have somehow be initializing Class1 before foo = MyApp() happens.</div><div>Is there a good reference on the order that things happen in python when a single script is run?</div><div><br></div>
<div>In the meantime here is my stripped down script (foo = app, bar = frame, object = graph_panel). I'd welcome all suggestions to reorganize it.</div><div><br></div><div><br></div></div>
</blockquote></div><br></div>