How do you subclass various wx.Windows?
Christopher Barker
Chris.Barker at noaa.gov
Fri Jan 5 16:50:39 PST 2007
Hi all,
When I subclass a wx.Window (could be a panel, Dialog, whatever), I
often need to pass in a small set of parameters. In addition to these,
I'd like to be able to pass in the usual wx.Window set: size, position,
flags, etc. I'm not sure the best way to do this, so I thought I'd see
what you all do.
For just a couple, I tend to do something like this:
class MyDialog(wx.Dialog):
def __init__(self, param1, param2, *args, **kwargs):
wx.Dialog.__init__(self, *args, **kwargs)
# Do something with param1 and param2
Which then needs to be called with:
MyDialog(p1,p2, parent, ....)
The problem with this is that param1 and param2 are non-keyword,
required arguments. That's usually fine if there are just a couple, but
if there are 6 or seven , and many are optional, I'm not sure what to
do. Also, we're all so used to passing the parent in first.
I could do:
class MyDialog(wx.Dialog):
def __init__(self, parent, param1=None, param2=None, **kwargs):
wx.Dialog.__init__(self, *args, **kwargs)
# Do something with param1 and param2
That would be OK, but then all the usual arguments need to be passed in
with keywords -- not too bad.
Another option is this:
class MyDialog(wx.Dialog):
def __init__(self, *args, **kwargs):
self.param1 = kwargs.pop("param1", None)
self.param2 = kwargs.pop("param2", None)
wx.Dialog.__init__(self, *args, **kwargs)
But that requires that all extra parameters be given as keyword args
(not too bad, I like to do that anyway), and also is a bit confusing,
and the function signature doesn't give you any info about what's expected.
Any thoughts?
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the wxpython-users
mailing list