From: Kevin A. <al...@se...> - 2004-06-07 21:50:34
|
On Jun 7, 2004, at 11:34 AM, Alex Tweedly wrote: > > Is there an easy way to "clone" a button ? > > I want to have a grid (array) of buttons (or ImageButtons), which=20 > should be all same type, size, etc.=A0=A0 Ideally, I'd use the "array"=20= > feature in the Resource Editor :-) > > Since that doesn't exist,=A0 I decide to create them in the program=20= > start-up, so that if I later want to change the size, spacing, etc. I=20= > can do so easily. > > I thought I might get lucky, and tried > > basename =3D self.component.baseBtn.name > (basex, basey) - self.components.baseBtn.position > for row in range(5): > =A0=A0 for col in range(5): > =A0=A0=A0=A0=A0 tBtn =3D copy.copy(self.components.baseBtn) > =A0=A0=A0=A0=A0 tBtn.name =3D basename + "_%02d%02d" * (row, col) > =A0=A0=A0=A0=A0 tBtn.position =3D (basey+50*row, basex + 50*col) > =A0=A0=A0=A0=A0 self.components[tBtn.name] =3D tBtn > > or something like that. Of course, that fails, the components are not=20= > a simple dictionary. > > So my next thought was more like > > basename =3D self.component.baseBtn.name > (basex, basey) - self.components.baseBtn.position > for row in range(5): > =A0=A0 for col in range(5): > =A0=A0=A0=A0=A0 tBtn =3D {} > =A0=A0=A0=A0=A0 # copy all existing attributes > =A0=A0=A0=A0=A0 for k,v in self.components.baseBtn.iteritems(): > =A0=A0=A0=A0=A0=A0=A0=A0 tBtn[k] =3D v > =A0=A0=A0=A0=A0 # and fix-up as needed > =A0=A0=A0=A0=A0 tBtn.name =3D basename + "_%02d%02d" * (row, col) > =A0=A0=A0=A0=A0 tBtn.position =3D (basey+50*row, basex + 50*col) > =A0=A0=A0=A0=A0 self.components[tBtn.name] =3D tBtn > > But that also fails - can't get an iteritems() from components. > > Any suggestions ? > You were on the right track. What you want is the starting resource=20 for the component or alternatively, iterate over the keys of the=20 attributes (baseBtn._spec.getAttributes()) and use the ones you want=20 from the component. If the component attributes may have changed since=20= initialization, then the associated resource description will be out of=20= sync, but that doesn't sound like an issue for what you're trying to=20 do. If you look at the Widget class in the widget.py module, you'll see=20= that the starting resource is stored in a private variable called=20 _resource. So what you want is to use that. Since _resource is actually=20= an instance of the Resource class, go ahead and grab the dictionary=20 directly, then modify as necessary for the new components you create. baseResource =3D self.component.baseBtn._resource.__dict__ # shallow copy tBtn =3D baseResource.copy() ... self.components[tBtn.name] =3D tBtn You can also look at the on_componentDuplicate_command and=20 copyWidgetDescriptionToClipboard methods in the resourceEditor.py file=20= if you want to get some ideas about how you could roll your own "create=20= array" command for the resourceEditor. ka= |