Re: Creating dynamic window

John Henry <[email protected]>
Newsgroups gmane.comp.python.pythoncard
Message-ID <[email protected]>
There are people at the Python newsgroup suggesting
that I should avoid using exec to accomplish this. 
Here's what they suggested:

#!/usr/bin/python

"""
__version__ = "$Revision: 1.6 $"
__date__ = "$Date: 2004/08/17 19:46:06 $"
"""

import new

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
} }

def mouseclick_factory(parent, name):
    id_num=int(name[-1:])
    parent.components[name] = {'type':'Button',
                                 'name':name,
                                 'label':name,
                                 'position':(5,
5+id_num*30),
                                 'text':name}
    def function(self, event):
        print "You clicked '%s'." % name
    function.name = "on_%s_mouseClick" % name
    return function

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'}
        name = "Button1"
        function = mouseclick_factory(self, name) # as
before
        method = new.instancemethod(function, self,
self.__class__)
        setattr(self, function.name, method)

if __name__ == '__main__':
    app = model.Application(Minimal, None, rsrc)
    app.MainLoop()

Unfortunately, it doesn't work.  PythonCard doesn't
calll the fuction created by the mouseclick_factory. 
However, if I place the following statement right
after the setattr call, it works (proving that the
function actually gets created).

        self.on_Button1_mouseClick(event)

Any idea why this doesn't work?

Thanks,

--
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/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.