From: Kevin A. <al...@se...> - 2004-09-03 20:45:58
|
On Sep 3, 2004, at 11:42 AM, Gregory Pi=F1ero wrote: > Hi, > > I'm looking at the dbrowser sample. On my computer this is the file: =20= > C:\Python23\Lib\site-=20 > packages\PythonCard\samples\dbBrowser\dbBrowser.py > > So this thing when running seems to know the number of columns in any =20= > table I connect to and then (creates?) that many text boxes to show =20= > the values of each column. Does anyone know how this would work? How = =20 > are the text boxes created on the fly like that? > > I was unable to locate any documentation on this this and I can't see =20= > how it does it from the code. > > Thanks, > > Greg Creating components on the fly is easy, you just need to provide a =20 dictionary of the key:value pairs. Just look at the dictionaries for =20 each component in any .rsrc.py file and you'll see the same kind of =20 dictionary in use. For example, to create a button named 'btn1' in your =20= code you might do something like: self.components['btn1'] =3D {'type':'Button', 'name':'btn1', =20 'position':(0, 30), 'label':'btn1 hello'} So, let's say you want to dynamically create 10 single-line fields when =20= your application starts up. Here's one way you might do it: def on_initialize(self, event): insetFromEdge =3D 5 padding =3D 4 self.components['field0'] =3D {'type':'TextField', =20 'name':'field0', 'position':(5, 5), 'text':'field0'} height =3D self.components.field0.size[1] for i in range(1,10): name =3D 'field' + str(i) position =3D (insetFromEdge, insetFromEdge + i * (height + =20= padding)) text =3D name self.components[name] =3D {'type':'TextField', 'name':name, = =20 'position':position, 'text':text} I'm creating the first TextField outside the loop so that I can get the =20= height of a TextField dynamically, which allows the code to work =20 regardless of the default text height on any given platform. You could =20= just as easily, initialize the height to 0 and do an if block after the =20= self.components[name] line to set the height. ka= |