From: Kevin A. <al...@se...> - 2007-06-22 21:47:45
|
On Jun 22, 2007, at 9:56 AM, kc1...@ya... wrote: > I've been searching the net for this topic and the one > thing that pops up is the addMethod routine. It > sounds like something I can use. However, there is no > information that I can find on how to use it to help > me. > > Could somebody please help. > > When I create a button on the fly - with a name > defined at run-time, how would I define a method for > it? > > Thanks, http://pythoncard.sourceforge.net/text/addmethod.txt addMethod was the way to do it in versions of PythonCard prior to release 0.8, but when I changed to event binding to a more efficient static system, rather than looking for event handlers each time an event fires, the old addMethod stopped working as described. Now, for some reason I thought that I was going to have to redo the "magic" code in model.py for the addMethod method, so I let this issue hanging. However, I just did a little experiment which seemed to work fine, so let me know if this works for you. Simply add your event handlers to your code BEFORE you create the components you want the events to bind to: >>> def on_btn1_mouseClick(self, event): ... self.components.field1.text = event.target.name ... >>> self.addMethod(on_btn1_mouseClick) >>> comp['btn1'] = {'type':'Button', 'name':'btn1', 'position':(0, 30), 'label':'btn1 hello'} Other than the last two lines this is identical to the way it used to work, we're simply making sure the event handler method is added to our code before the component is created so its event binding works correctly. Now, assuming I didn't just get lucky, this is pretty nice that it works again, but this example is done totally in the shell with the minimal.py sample application. What I can't remember offhand, is which Python module and method you need to use to pass in an arbitrary string and get back a function that we can do something with. For example, compiling... c = someComboOfCompileAndEval('def on_btn1_mouseClick(self, event): \n self.components.field1.text = event.target.name\n\n') self.addMethod(c) Maybe it is a combination of compile and eval, but the correct globals and locals will have to be passed in order for it to make sense as a method. I could probably dig into the Python interpreter to see what it is doing with the text at the shell prompt, but if you know, please post to the list. Thanks, ka |