Steve Freedenburg wrote:
Refectoring my configuration frame shrunk the code many, many lines.
The dictionaries that were created to the list worked like a champ...
Is there a way to refactor event binding and event generation? I asked this before
but I think the way I was asking the question was wrong, or the answers I was
given were beyond me, or lost in the thread somewhere.
I have 12 lines that look like this:
self.Bind(wx.EVT_BUTTON, self.C1DefSnd, self.Containers[0]['SndBtn'])
...
self.Bind(wx.EVT_BUTTON, self.C6DefSnd, self.Containers[5]['SndBtn'])
and
self.Bind(wx.EVT_BUTTON, self.C1DefFnt, self.Containers[0]['FntBtn'])
...
self.Bind(wx.EVT_BUTTON, self.C6DefFnt, self.Containers[5]['FntBtn'])
and 6 functions for the event DefSnd, and 6 functions for the event DefFnt
... I can't write:
def self.Functions[0]['SndDef'](self, event): Or can I?
No. The key is realizing that the "event" parameter that is given to your event function contains the button that was hit. You can use one handler for all 6 buttons. Inside the handler, if you say this:
def handler( self, event ):
btn = event.GetEventObject()
then "btn" will be the wx.Button that was originally bound to the event. So, if you store an extra piece of information in the button object, you can get it back. For example:
self.Containers[0]['SndBtn'].Container = self.Containers[0]
self.Bind( wx.EVT_BUTTON, self.handler, self.Containers[0]['SndBtn'] )
...
def handler( self, event ):
btn = event.GetEventObject()
cntr = btn.Container
Now "cntr" is the dictionary corresponding to this button.
···
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.