Returning Ref. to Runtime Created Control

F. GEIGER fgeiger at datec.at
Sun Sep 12 14:57:42 EDT 2004


"Gary" <probably.a.bogus.address at wi.rr.com> schrieb im Newsbeitrag
news:di49k0945hi9n73nkc0cnajk2af0e9k2mm at 4ax.com...
> Good afternoon:
>
> I've playing with dynamically created buttons in Boa Constructor using
> wxPython:
>
> def OnBtnButton(self, event):
>         btnIds = []
>         self.btn2 = []
>         xx = 24; yy = 42
>         for cntr in range(168):
>             self.btn2.append(wxButton(self.panel1, -1, str(cntr + 1),
>             wxPoint(xx, yy), wxSize(23, 23), 0))
>             rr = self.btn2[cntr].GetId()
>
>             yy += 26
>
>             if (yy + 44) > 380: # Nearing bottom of frame
>                 yy = 42             # Back to top of frame
>                 xx += 26           # starts new column
>
>             btnIds.append(str(rr))
>
>         func = ['self.BtnFunc1', 'self.BtnFunc2', 'self.BtnFunc3'..]
>
>         for ew in range(24):
>             EVT_BUTTON(self, self.btn2[ew].GetId(), eval(func[ew]))
>
> def BtnFunc1(self, event):
>         self.t1.SetValue("btnOne")
>
> def BtnFunc2(self, event):
>         self.t1.SetValue("btnTwo")
>
> def BtnFunc3(self, event):
>         self.t1.SetValue("btnThree")
>
> # etc.
>
> How can I press one of the buttons and have it return a reference to
> itself? (Some other languages have something like root._name or
> self._name). Thanks.
> --

def _onAnyOfMyButtons_(self, event):
   theResponsibleButton = event.GetEventObject()
   return

This way you only need *one* handler for hundreds of buttons.

You could derive your own button so that you could write:

def _onAnyOfMyButtons_(self, event):
   theResponsibleButton = event.GetEventObject()
   theResponsibleButton.writeYourDataToMyControl(self.t1)
   return

Or if your subclassed button own an attribute 'index':

def _onAnyOfMyButtons_(self, event):
   theResponsibleButton = event.GetEventObject()
   self.t1.SetValue("Button %d was pressed. " % theResponsibleButton.index)
   return

HTH
Franz GEIGER





More information about the Python-list mailing list