wxPython style question

Cliff Wells LogiplexSoftware at earthlink.net
Mon Nov 4 19:00:35 EST 2002


On Mon, 2002-11-04 at 12:24, Gary Pajer wrote:
> 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?

While either approach will work, maintaining unique names for windows
would seem to be a major PITA (I rarely even bother changing the default
name argument when using wxPython).  The approach I usually use is to
save references to windows that I know I'll need later on (or more
likely, add the references as I need them), and not save them for ones I
don't.  This gives you easy access to the objects you need while not
creating "attributes that might serve no purpose."

BTW, if you're *that* concerned about aesthetics, avoid GUI programming
- you'll go mad trying to balance your sensibilities with the
possibility of getting anything useful done ;)

> Am I missing some key point?

GUI programming sucks.  wxPython just makes it suck less.

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308





More information about the Python-list mailing list