Re: Creating dynamic window - addMethod revisited
John Henry <[email protected]>
| Newsgroups | gmane.comp.python.pythoncard |
|---|---|
| Message-ID | <[email protected]> |
Thanks for the response. While waiting, I experimented and I came up with a Python way (as oppose to the PythonCard way) to do it. See attached file. When you run the attached program, each time a button is pushed, a new button is created. When that new button is pushed, yet another new button gets created...so on and so forth... Ain't Python fun? --- Kevin Altis <[email protected]> wrote: > > On Jun 22, 2007, at 9:56 AM, > [email protected] 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 > > > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 > express and take > control of your XML. No limits. Just data. Click to > get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Pythoncard-users mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/pythoncard-users > -- John Henry -- John Henry ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ Pythoncard-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/pythoncard-users
noresource.py
(text/x-python, 1.2 KB)
#!/usr/bin/python
"""
__version__ = "$Revision: 1.6 $"
__date__ = "$Date: 2004/08/17 19:46:06 $"
"""
from PythonCard import model
rsrc = {'application':{'type':'Application',
'name':'Minimal',
'backgrounds': [
{'type':'Background',
'name':'bgMin',
'title':'Minimal PythonCard Application',
'size':(200, 300),
'components': [
] # end components
} # end background
] # end backgrounds
} }
class Minimal(model.Background):
def on_initialize(self, event):
self.components['field1'] = {'type':'TextField','name':'field1','position':(5, 5),'size':(150, -1),'text':'Hello PythonCard'}
return
text="class MinimalChild(Minimal):"
text += "\n\t" + "def on_initialize(self, event):"
text += "\n\t" + "\tMinimal.on_initialize(self,event)"
nButton = 5
for ix in xrange(nButton):
iButton=ix+1
text += "\n\t" + "\tself.components['Button"+str(iButton)+"'] = {'type':'Button', 'name':'Button"+str(iButton)+"', 'position':(5, "+str(5+iButton*30)+"), 'label':'Button"+str(iButton)+"'}"
if ix!=nButton-1:
text += "\n\t" + "def on_Button"+str(iButton)+"_mouseClick(self, event, name=None):"
exec(text)
if __name__ == '__main__':
app = model.Application(MinimalChild, None, rsrc)
app.MainLoop()