Using an object inside a class

Ian Kelly ian.g.kelly at gmail.com
Mon Jan 23 17:57:00 EST 2012


On Mon, Jan 23, 2012 at 3:45 PM, Jonno <jonnojohnson at gmail.com> wrote:
> I see, so that would get me access to the app instance during init of Class1
> but if I can't access frame or the object as they still aren't created yet.
> I can only do that in attributes that I know won't be called until the app
> is created.

Yeah, that's a good point.  In that case, that code really doesn't
belong in the Class1 __init__ method, as it depends on the whole
hierarchy having already been properly initialized.  Instead, I
suggest using the wx.CallAfter function to schedule that code for when
the event loop has actually been started.  For example:

class Class1(wx.Panel):
    def __init__(self, parent, id = -1, dpi = None, **kwargs):
        # Do all your initialization stuff.  Then:
        wx.CallAfter(self._init_graph_panel)

    def _init_graph_panel(self):
        wx.GetApp().frame.graph_panel.plot([1,2,3,4,5],[3,4,3,4,3])
        wx.GetApp().frame.graph_panel.figure.canvas.draw()


By the way, looking at your object hierarchy more closely, isn't
"app.frame.graph_panel" going to end up being the same thing as just
"self.figure"?  Why not just use the latter and remove the reliance on
finding the correct frame?

Cheers,
Ian



More information about the Python-list mailing list