Convoluted question

Peter Yu roger_callisto at hotmail.com
Mon Jul 31 21:32:17 EDT 2000


In article <75F7304BB41CD411B06600A0C98414FCB3682D at ORSMSX54>,
  "Daley, Mark W" <mark.w.daley at intel.com> wrote:
> I am a little frustrated by this situation.  I have written some code
to
> take an indeterminate number of server names to create a column of
check
> buttons:
<code and exception snipped>
> I think this comes from the fact that I didn't define the names in
varlist
> as variables.  It seems to me that there should be an easy way to
assign
> variable names for each check button in an automated way, and an easy
way to
> return the status of a check button.

Yup. =) It's not only possible, but quite easy.
Given your snippet, try the following:
(I'll assume the code's not in a class, though it really ought to be.
 If it is, replace all instances of checkholder with self and don't
 worry about declaring blank. =))

servername = ['Server1', 'Server2', 'Server3', ..., 'Servern']
class blank: #Blank class. Or is there a way to reference current env?
   pass

checkbuttons = blank() #Create an empty holder class.

for item in range(len(servername)):
   button_name = 'button%d' % item
   setattr(checkholder, button_name, IntVar())
   b1 = Checkbutton(root, text = servername[item],
                    variable = getattr(checkholder, button_name)
   b1.grid(row = item, column = 0, sticky = W)


Then you can check the values via getattr(checkholder, 'button0')
for the very first value, for example.

Hope this was helpful. =)
Peter Yu


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list