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 |