Re: Creating dynamic window

John Henry <[email protected]>
Newsgroups gmane.comp.python.pythoncard
Message-ID <[email protected]>
Okay, here's a version that works, clean, and not
requiring any changes to PythonCard.

#!/usr/bin/python

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

import new

from PythonCard import log
from PythonCard import model
from PythonCard.model import SetInitParam

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 Background_Dynamic(model.Background):

    def __init__(self, aParent, aBgRsrc,
SetInitParamFct=SetInitParam):
        model.Background.__init__(self, aParent,
aBgRsrc, SetInitParamFct)
        
    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 mouseclick_factory(self, name):
        def function(self, background, event):
           
self.mouseclick_factory("Button"+str(int(name[-1:])+1))
        function.name = "on_%s_mouseClick" % name
        method = new.instancemethod(function, self,
self.__class__)
        setattr(self, function.name, method)
        self.addHandler(method)
        self.components[name] = {'type':'Button',
                                     'name':name,
                                     'label':name,
                                     'position':(5,
5+int(name[-1:])*30),
                                     'text':name}
        return function
    
class Minimal(Background_Dynamic):
    def on_initialize(self, event):
        self.components['field1'] =
{'type':'TextField','name':'field1','position':(5,
5),'size':(150, -1),'text':'Hello PythonCard'}
        self.mouseclick_factory("Button1")

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


--- Kevin Altis <[email protected]> wrote:

> After a bit of research I came up with the following
> working example  
> which uses a slightly modified version of your
> original factory.  
> func_name seems to be the important attribute. I was
> able to use  
> addMethod to clean up the code a bit .
> 
> This still doesn't solve the problem of creating an
> arbitrary  
> function/method from a string, but that's a generic
> Python problem  
> that I don't know the answer to rather than
> something PythonCard- 
> specific.
> 
> ka
> ---
> 
> from PythonCard import model
> 
> rsrc = {'application':{'type':'Application',
>            'name':'Minimal',
>      'backgrounds': [
>      {'type':'Background',
>            'name':'bgMin',
>            'title':'Minimal PythonCard Application',
>            'size':(200, 100),
>           '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'}
>          self.mouseclick_factory("Button1")
>          self.mouseclick_factory("Button2")
> 
>      def mouseclick_factory(self, name):
>          def function(self, event):
>              # changed to event.target.name to
> verify we're getting
>              # the correct target when button is
> clicked
>              print "You clicked '%s'." %
> event.target.name
>          # func_name seems to be the magic attribute
> rather than
>          # just setting func.name
>          function.func_name = "on_%s_mouseClick" %
> name
>          self.addMethod(function)
>          self.components[name] = {'type':'Button',
>                                       'name':name,
>                                       'label':name,
>                                      
> 'position':(5,5+int(name[-1:]) 
> *30),
>                                       'text':name}
>          return function
> 
> 
> if __name__ == '__main__':
>      app = model.Application(Minimal, None, rsrc)
>      app.MainLoop()
> 
> 
>
-------------------------------------------------------------------------
> 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

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