wxPython: How to get a child by name?

Robin Dunn robin at stop.spam.alldunn.com
Tue Mar 27 17:04:26 EST 2001


"Franz GEIGER" <fgeiger at datec.at> wrote in message
news:99qpce$ljk$1 at newsreaderg1.core.theplanet.net...
> I currently try to polish some of my apps with a GUI and decided to go for
> wxPython.
>
> I use wxDesigner for the layout. As it generates code after every change,
I
> named all the text controls and checkboxes etc. of the dialog hosting all
> those controls by applying SetName() to the controls. The Dialog is small,
> so I do not use validators yet.
>
> To get a text control of the dialog I thought I could write
> theValue = self.FindWindowByName("tcSurname").GetValue().
> This raises an exception, because the return value is of type wxWindowPtr
> which does not have such a method.

...

> For a "quick" hack in a MSVC project I would downcast the returned pointer
> of GetDlgCtrl() to be able to call the subclassed object's methods. But
how
> do I proceed here?


You're on the right track, you're looking for wxPyTypeCast which takes a
shadow object and converts the swigified pointer within it to a different
type and wraps a new shadow object around it of the new type.  There are
some comments about it in wx.py.  (I'm making some changes soon that will
hopefully let me finally turn this hack into a no-op...)

But since you are using wxDesigner you could have saved yourself a bunch of
work and let it generate some "getters" for you.  Click on the Add Python
Source button to add a new sourcefile to your project, then double click the
filename under Python sources in the tree view to open the file.  Once there
you can add classes that create and use the specific forms you've designed,
and also add to the class event handlers and getters for the fields on the
form.  I usually end up changing a lot of the code that it generates for the
__init__ method but that's okay since it never regenerates this file, only
adds to it.  For the getters it makes code like this:

    def GetLoginBitmap(self):
        return wxPyTypeCast( self.FindWindowById(ID_LOGINBITMAP),
"wxStaticBitmap" )

    def GetPasswd(self):
        return wxPyTypeCast( self.FindWindowById(ID_PASSWD), "wxTextCtrl" )

    def GetUsername(self):
        return wxPyTypeCast( self.FindWindowById(ID_USERNAME),
"wxTextCtrl" )

    def GetURL(self):
        return wxPyTypeCast( self.FindWindowById(ID_URL), "wxComboBox" )

You just need to be sure that you give each control a unique ID name.  Once
you have the getters like this you can write code like:

    self.GetUsername().GetValue()


--
Robin Dunn
Software Craftsman
robin at AllDunn.com       Java give you jitters?
http://wxPython.org      Relax with wxPython!







More information about the Python-list mailing list