[wxPython-users] calling functions from a different window

Michael Barron barronmo at gmail.com
Sat Dec 8 12:37:08 PST 2007


Mark:

Thanks for the help, excellent teaching.  I've got things working based on
your examples; doesn't seem too hard now but I was missing a few of the
details.

Mike

On Dec 6, 2007 3:33 PM, Mark Erbaugh <mark at microenh.com> wrote:

> On Thu, 2007-12-06 at 13:12 -0600, Michael Barron wrote:
> > As someone who is new to programming I am still struggling with doing
> > things between windows.  The namespace issues give me a lot of
> > trouble.  Robin Dunn had a good response on 30 Nov about transferring
> > data between a dialog and a frame.  I was wondering about calling
> > methods from a different class.  I have a dialog that I want to do
> > several things on a notebook page but I can't seem to find the right
> > way to do it.
> >
> > So far I've tried calling the method using the instance names for the
> > notebook page's class.  This ends up looking like
> > "app.frame.nb.page3.UpdatePage()."  I get an error saying 'app' is
> > unknown.  I've also tried called .GetTopLevelParent() but this
> > apparently is the dialog itself; no parent I guess.  I read in the
> > book about finding windows by their id, name or label but I'm not sure
> > how to do this.  I've messed around on PyCrust with passing instance
> > names to other classes in order to call their methods but doing this
> > in a GUI environment is difficult for me.
> >
> > This subject seems like a good one for a tutorial.  Is there one out
> > there I am missing?
> >
> > Thanks for the help.
>
> Mike,
>
> The first thing you need to get your head around is the concept of
> classes and instances.  When you define a Frame, you are usually
> sub-classing the existing wx.Frame class. This creates a new class.
> However, when your code runs it actually creates an instance of this
> class.  Think of the class as the template or pattern for an object, an
> instance is the actual object created from that template. It is common
> to create more than one instance of the same class.
>
> In Python (and wxPython) an instance is created when you "call" the
> class name.  If your frame class is CompanyFrame, you probably have code
> like:
>
> frameCompany =3D CompanyFrame(None, -1, '')
> frameCompany.Show()
>
> Your parameters in the parenthesis may be slightly different. The first
> statement creates an instance of the CompanyFrame class and stores a
> reference to it in the frameCompany variable.  The second line then
> calls the Show method of this instance.
>
> In some cases, you may combine the two and have a statement like:
>
> CompanyFrame(None, -1, '').Show()
>
> This creates the instance and calls the Show method. The difference is
> that it does not create a reference that you can work with.  I suspect
> that this is your problem.
>
> What I would recommend is that you create a reference as in the first
> example. Then pass that reference to the dialog class instance.  For
> example, Assume your dialog class is DialogClass
>
> dialog =3D DialogClass(...)
> dialog.company_frame =3D frameCompany
>
> Then in the dialog you can access the items in the CompanyFrame instance
>
> self.company_frame.nb.page3.UpdatePage().
>
> As an aside, I wonder if nb.page3 is the correct way to access that
> widget. While visually, page3 is on the notebook and the notebook is on
> the form, the way I typically design a screen, the page3 widget is a
> data member of the frame, not a data member of the notebook widget, so
> the reference would be company_frame.page3.UpdatePage(), leaving out the
> nb.
>
> I hope this helps.
>
> Mark
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe at lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help at lists.wxwidgets.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200712=
08/7fe73084/attachment.htm


More information about the wxpython-users mailing list