From: Alex T. <al...@tw...> - 2004-06-07 18:29:29
|
--- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.699 / Virus Database: 456 - Release Date: 04/06/2004 |
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= |
From: Kevin A. <al...@se...> - 2004-06-12 23:26:21
|
On Jun 12, 2004, at 1:49 PM, Alex Tweedly wrote: > At 14:50 07/06/2004 -0700, Kevin Altis wrote: > >> On Jun 7, 2004, at 11:34 AM, Alex Tweedly wrote: >> >> >>> Is there an easy way to "clone" a button ? [ ... ] >>> Any suggestions ? >> You were on the right track. What you want is the starting resource >> for the component or alternatively, iterate over the keys of the >> attributes (baseBtn._spec.getAttributes()) and use the ones you want >> from the component. > > Thanks for the help Kevin (took me a few days to get time to try it > all out properly). > > I ran into a problem with this first suggestion - couldn't figure out > how to get the info I needed out of it - decided to wait until I had > more experience looking through the PCard source before tackling this > one. On to Plan B ... Check out the code in samples/widgets/widgets.py to see how getAttributes is used to create component spec docs. That should give you some ideas. > >> If the component attributes may have changed since initialization, >> then the associated resource description will be out of sync, but >> that doesn't sound like an issue for what you're trying to do. If you >> look at the Widget class in the widget.py module, you'll see that the >> starting resource is stored in a private variable called _resource. >> So what you want is to use that. Since _resource is actually an >> instance of the Resource class, go ahead and grab the dictionary >> directly, then modify as necessary for the new components you create. > > widget.py has no mention of a variable called "_resource" My bad on the suggestion above, I was working from the PythonCard source in cvs which will become PythonCard release 0.8. As you found the _resource instance variable isn't in release 0.7.3.1. >> baseResource = self.component.baseBtn._resource.__dict__ >> # shallow copy >> tBtn = baseResource.copy() >> ... >> self.components[tBtn.name] = tBtn > > and naturally this complained about _resource. > OK - once again I need to spend some time reading through source code > before I'm ready for this one. > On to Plan C ... > >> You can also look at the on_componentDuplicate_command and >> copyWidgetDescriptionToClipboard methods in the resourceEditor.py >> file if you want to get some ideas about how you could roll your own >> "create array" command for the resourceEditor. > > This worked a treat ! > I didn't actually add anything to the resource editor - but lifted the > code from there to put into my application. I now have a nice little > function that takes a button and a static box, and spreads an array of > buttons to fill the box. > > My "Boggle" game is much cleaner now - Thanks again. > > -- Alex Tweedly. > That sounds like a fun sample. If you want to post a URL where people can download it I'll add it to the PythonCard More Apps page or if suitable we can even make it a sample and include it with PythonCard. http://pythoncard.sourceforge.net/moreapplications.html ka |
From: Alex T. <al...@tw...> - 2004-06-15 18:23:57
|
I replied on this thread a couple of days ago, but the message didn't get to the list (it did get to Kevin - he replied and it's now a separate thread .... sorry for any confusion in the archives). Here's the final result ... At 16:25 12/06/2004 -0700, Kevin Altis wrote: >That sounds like a fun sample. If you want to post a URL where people can >download it I'll add it to the PythonCard More Apps page or if suitable we >can even make it a sample and include it with PythonCard. It's now available (source code only) at www.tweedly.net/Python -- Alex Tweedly. |