|
From: John H. <ec...@ya...> - 2007-06-26 22:57:20
|
redirected from users mailing list for future reference on this type
of problem...
ka
---
Man, life is gooooooooooood....
By creating controls dynamically, my efficiency in
creating GUI code went up many many fold. I must have
created 50 different panels in less than a day -
including code for designing, presentation, and
processing of user input. I can not imagine how long
it would have taken me to do the same thing using the
static method.
Just for completeness, I am posting the final version
of my Control_Factory code, along with code for
destruction of controls on the fly. May be this code
would be of use to other PythonCard users:
#!/usr/bin/python
"""
__version__ = "$Revision: 1.6 $"
__date__ = "$Date: 2004/05/05 16:53:27 $"
"""
import wx
from PythonCard import model
import new
from PythonCard import log
from PythonCard import model
class Minimal(model.Background):
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 delHandler(self, aMethod):
# Add the Handler to our Handler list.
if aMethod.name in self._handlers:
log.debug("delHandler: " + aMethod.name)
del self._handlers[aMethod.name]
del aMethod
def Control_factory(self, attribute, eventHandlers):
name=attribute['name']
methods=[]
for eventName in eventHandlers.keys():
eventFct=eventHandlers[eventName]
def function(self, background, event):
if eventFct==None:
return None
return eventFct(event)
function.name = "on_%s_%s" % (name,eventName)
method = new.instancemethod(function, self,
self.__class__)
setattr(self, function.name, method)
self.addHandler(method)
methods.append(method)
self.components[name] = attribute
return methods
To use it, I simply set up a table like this:
({"type":"StaticText","name":"stFIELD1","text":"This
is easy:",'position':(5, 10), },{}),
({"type":"TextField",
"name":"tfFIELD1",'position':(35, 10-4),
'size':(70,-1)},{}),
({"type":"Button",
"name":"pbBUTTON",'label':'Execute', 'position':(70,
10+75)},
{"mouseClick":self.on_pbBUTTON_mouseClick}),
and then invoke Control_factory repeatedly.
Thanks for a wonderful package PythonCard. Couldn't
do this work without it.
> -----Original Message-----
> From: pyt...@li...
>
[mailto:pyt...@li...]
On
> Behalf Of John Henry
> Sent: Monday, June 25, 2007 10:57 AM
> To: Kevin Altis; pythoncard-Users
> Subject: Re: [Pythoncard-users] Creating dynamic
window
>
>
> Oh, silly me. Control_factory can be further
> simplied:
>
> def Control_factory(self, attribute, eventHandlers):
> name=attribute['name']
> for eventName in eventHandlers.keys():
> eventFct=eventHandlers[eventName]
> def function(self, background, event):
> if eventFct==None:
> return None
> return eventFct(self, event)
> function.name = "on_%s_%s" % (name,eventName)
> method = new.instancemethod(function, self,
> self.__class__)
> setattr(self, function.name, method)
> self.addHandler(method)
> self.components[name] = attribute
> return function
>
--
John Henry
|