From: John H. <ec...@ya...> - 2007-06-25 17:56:35
|
Oh, silly me. Control_factory can be further simplied: def Control_factory(self, attribute, eventHandlers): name=attribute['name'] for eventName in eventHandlers.keys(): eventFct=eventHandlers[eventName] def function(self, background, event): if eventFct==None: return None return eventFct(self, event) function.name = "on_%s_%s" % (name,eventName) method = new.instancemethod(function, self, self.__class__) setattr(self, function.name, method) self.addHandler(method) self.components[name] = attribute return function --- John Henry <ec...@ya...> wrote: > > Hmm, the code I sent to the list worked perfectly > on > > my machine. > > Don't you just love software development? > > > Did > > you try and run the code as is? > > Yes I did. Same failure. > > > Which version of > > Python are you > > using? > > Latest version of Python 2.3 under Windows XP w > latest > FP > > >I was trying it with 2.4.3 on a Mac, but if > > you're using 2.5 > > maybe they added additional flags you're supposed > to > > use at the > > command-line so that Python doesn't give you a > > warning?! I'm also a > > bit confused about the multiple args. I added the > > line... > > > > print "You clicked '%s'." % event.target.name > > > > to show that the event being passed in is what > we're > > expecting and > > again on my box it works as expected. > > > > ka > > > > > > That part doesn't bother me. > > Now, I've played around the idea more and I came up > with a version that works in the general case - for > all controls, and in-line with the resource > structure > of PythonCard. See below: > > def addHandler(self, aMethod): > # Add the Handler to our Handler list. > if aMethod.name not in self._handlers: > log.debug("addHandler: " + aMethod.name) > #self._handlers[aMethod.name] = > event.Handler(aMethod) > self._handlers[aMethod.name] = aMethod > > def Control_factory(self, attribute, > eventHandlers): > name=attribute['name'] > for eventName in eventHandlers.keys(): > eventFct=eventHandlers[eventName] > def function(self, background, event): > if eventFct==None: > return None > return eventFct(self, event) > function.name = "on_%s_%s" % (name,eventName) > method = new.instancemethod(function, self, > self.__class__) > setattr(self, function.name, method) > self.addHandler(method) > self.components[name] = {} > for key in attribute.keys(): > self.components[name][key]=attribute[key] > return function > > > Now, everytime I need to create a control - any kind > of PythonCard controls, I simply do a: > > # This is my button mouseClick handler > def on_Button_mouseClick(self, event): > return > > Create a button: > > Control_factory(self, attribute={"type":"Button", > "name":"Button1", > "label":"Button1","position":(5,35)}, > eventHandlers={"mouseClick":on_Button_mouseClick}) > > Notice that the attribute is a standard dictionary > no > different from that in the resource file. > > And it works for all controls. Some form of this > should exist in standard form of PythonCard. > > -- > John Henry > -- John Henry |