[Tutor] Dynamic-ish naming of variables?

Liam Clarke cyresse at gmail.com
Mon Nov 22 08:58:13 CET 2004


Hi all,

As I've been following the tutor group, this has come up from time to
time, and the usual response has been, why would you want to?

Well, here's why. I'm building a GUI with PythonCard, and I want to
allow the user to edit a list of people, which will then display or be
removed from the window.

Easy enough to do.

But, what I want to do is to have a checkbox and choice box for each name. 

PythonCard uses a resource file to define widgets, and it would look like this -

{'type':'Choice', 
    'name':'Choice1', 
    'position':(117, 112), 
    'items':[], 
    },

{'type':'CheckBox', 
    'name':'CheckBox1', 
    'position':(104, 71), 
    'checked':True, 
    'label':'CheckBox1', 
    },

So, I can use Python to write in widgets to that code or remove them,
simple enough concept.

My trouble is the name part.

I haven't used dictionaries that often, or would a list be better?

When the user clicks 'save' I want Python to move down the list,
checking to see if each checkbox has been checked or not, and what the
value of the choice box is.

I figure simplest way to 'dynamically' create my widgets is to
reference them by the name added -

i.e. the name "Liam Clarke" added would create the following - 

{'type':'Choice', 
    'name':'LiamClarkeChoice', 
    'position':(117, 112), 
    'items':[], 
    },

{'type':'CheckBox', 
    'name':'LiamClarkeCheck', 
    'position':(104, 71), 
    'checked':True, 
    'label':'CheckBox1', 
    },

Python checks values assigned by the following - 

if self.components.LiamClarkeCheck.checked=True ....

How do I do the name part randomly, short of having my module re-write itself?

Ideally it would iterate over a list, something along these pseudo-lines -

for name in nameList:

     if self.components.%sCheck.checked=True % name
     teamDict[name]=self.components.%sChoice.field % name

That's impossible of course, but it's what I'd like to do.

Any suggestions? Could I do something like the above and use eval /
exec? I'm not too familiar with them.

Thanks in advance,

Liam Clarke



-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list