Re: Creating dynamic window

John Henry <[email protected]>
Newsgroups gmane.comp.python.pythoncard
Message-ID <[email protected]>
Thanks for the response.

The code almost worked.  The offending line is:

function.func_name = "on_%s_mouseClick" % name

which is a TypeError violation (readonly attribute). 
I ran into this too when I try to set method.__name__
directly.

The workaround, as it seems, is to have an addHandler
function (rather then the _addHandler function) that
does this: 

    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

instead of this:

    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


When I call addHandler then, the code works.  When I
click the first button, a new button gets created. 
One minor puzzle though, Python passes down 3
parameters to the handler (rather then just 2).  The
first 2 are both the same and it's the normal "self"
parameter you get with other handlers.  The 3rd one is
the event object.  I don't know why I get "self" twice
though.




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