tkinter radiobutton

Peter Otten __peter__ at web.de
Wed Jun 29 01:26:26 EDT 2005


William Gill wrote:

> Also, does 'row == var.get() for var in self.variables' perform the
> comparison row == var.get() for each item in self.variables?  I would
> have had to write:
> 
> for var in self.variables:
>     return row == var.get()

Or rather

result = []
for var in self.variables:
    result.append(row == var.get())
return tuple(result)

This can be rewritten to a 'list comprehension'

return tuple([row == var.get() for var in self.variables])

and, since Python 2.4, to the 'generator expression' that I used and which
avoids building the intermediate list. Both constructs also feature an
if-clause, see

http://docs.python.org/tut/node7.html#SECTION007140000000000000000
http://docs.python.org/tut/node11.html#SECTION00111100000000000000000

> p.s.  I tweaked
> 
> rbn = tk.Radiobutton(self, text=text, variable=var, value=y)
> to
> rbn = tk.Radiobutton(self, text=text, variable=var, value=y+1)
> 
> and
> 
> return tuple(row == var.get() for var in self.variables)
> to
> return tuple(row+1 == var.get() for var in self.variables)
> 
> so that the Radiogrid doesn't initialize w/row 1 selected, and
> accomodates cases where nothing is selected in any column.
 
Another option would have been to initialize the variables

...
var = tk.IntVar()
var.set(-1)
if trace_write:
...

Peter




More information about the Python-list mailing list