wxPython style question

Gary Pajer pajer at iname.com
Mon Nov 4 15:24:14 EST 2002


I'm trying to learn my way around wxPython.  I'm relatively new to python.

I have (at least) two choices when it comes time to create an object (say a
wxPanel). The first is what most demos seem to do:  I can make it an
instance attribute of the Frame (referred to by self):

def  __init__(self, ...):
      self.panel1 = wxPanel(self, ...)
      etc.

And then reference to the Panel is always available via self.panel1

  Or I can call the constructor without "self" in which case (if I
understand the mechanics) the wxWindow object is created, but the wxPython
object that wraps it dissapears after __init__ finishes.

def __init__(...):
       panel1 = wxPanel(self, name="panel1", ... )
       etc.


Now,  if I want to change an attribute of the Panel:
In the former case,  I call

self.panel1.wxSetWhatever().    (where self is the wxFrame instance)


In the later, I need to find the wxWindow version of the Panel.  I can do
this by giving the Panel a name at creation, and then searching for the name
later, e.g. in a callback routine:

def  ProcessMenuSelection(self,event):
       panel = self.FindWindowByName("panel1")
       panel.wxSetWhatever()
       etc.

The later method has some asthetic appeal to me because it does not litter
the source code with so many  selfs, and it doesn't create attributes that
might serve no purpose.
These reasons may not be good reasons.  Is there some reason for preferring
one approach over the other?
Am I missing some key point?

TIA,
Gary







More information about the Python-list mailing list