Convoluted question

Daley, Mark W mark.w.daley at intel.com
Mon Jul 31 18:30:38 EDT 2000


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:

from Tkinter import *
import dbi, odbc, time, os, sys, Pmw

servername = ['Server1', 'Server2', 'Server3', ..., 'Servern']


for item in range(len(servername)):
	b1 = Checkbutton(root, text = servername[item], variable = var)
	b1.grid(row = item, column = 0, sticky = W)


I am at a loss for how to differentiate the variable names in the loop.  One
suggestion was to use a dictionary, although there were no details involved
in that suggestion.  Another was to do something like this (pseudocode,
thank you, Michal):

btnLst = []
for x in range(whatever):
   newBtn = SomeButton() # just create the widget
   btnValue =  SomeValue() # read from a list, maybe?
   newBtn.pack() # or whatever to get it on the screen
   list.append((newBtn,btnValue)

and later on:

for button in btnLst:
    if button.isChecked:
        DoWhatever()

I attempted to use a list of variable names to be assigned as needed when
creating the checkbuttons, but I keep getting NameErrors when I go to print
the values:

servername = ['Server1', 'Server2', 'Server3', ..., 'Servern']
varlist = ['var0', 'var1', 'var2', ..., 'varn']

for item in range(len(servername)):
	b1 = Checkbutton(root, text = servername[item], variable =
varlist[item])
	b1.grid(row = item, column = 0, sticky = W)

Exception in Tkinter callback
Traceback (innermost last):
  File "C:\Program Files\Python\Lib\lib-tk\Tkinter.py", line 764, in
__call__
    return apply(self.func, args)
  File "C:\PYTHON\gui.py", line 14, in showvalues
    print var0
NameError: var0

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.  I haven't found either after almost a
full day of coding and poring over different texts, such as 'Python
Essential Reference' and 'Python and Tkinter Programming'.  Is what I want
possible, or do I have to use what seems to me to be a more 'brute force'
method?

TIA

- Mark
----------------------------------------------
The opinions expressed are mine, and are not necessarily those of my
employer.






More information about the Python-list mailing list