tkinter radiobutton

William Gill noreply at gcgroup.net
Tue Jun 28 17:16:51 EDT 2005


 > What follows looks more like a spec than a question.
It is, but  I wanted to show why I was getting confused trying to use 
control variables to maintain the overall relationship.

Thank you.  This is exactly what I'm trying to do, and oddly enough 
similar in concept to what I was doing, but far more readable and 
maintainable (thus the philosophical component :-))
using 'for x, column in enumerate(columns):' for a looping structure
cleared up a lot of the convoluted 'score keeping' I was trying to do in 
my nested loops.

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()

Again, thanks.

Bill

Peter Otten wrote:
> William Gill wrote:
> 
> 
>>I thought the problem was practical, not philosophical, but what do I
>>know I'm the one asking for help.
> 
> 
> What follows looks more like a spec than a question.
> 
> 
>>       columns can have 0 or 1 selection
>>       rows can have 0,1,2,3, or 4 selections.
> 
> 
>>Loop through the 4 intVars 4 times; compare their value to the value for
>>the row being processed; if they are the same bitor a value to a
>>rowVariable i.e. convert the column information (intVar values) to row
>>information.
> 
> 
> Here's my implementation: 
> 
> import Tkinter as tk
> 
> class Radiogrid(tk.Frame):
>     def __init__(self, master, columns, trace_write=None):
>         tk.Frame.__init__(self)
>         self.variables = []
>         self.buttons = []
>         for x, column in enumerate(columns):
>             var = tk.IntVar()
>             if trace_write:
>                 var.trace_variable("w", trace_write)
>             self.variables.append(var)
>             self.buttons.append([])
>             for y, text in enumerate(column):
>                 rbn = tk.Radiobutton(self, text=text, variable=var, value=y)
>                 rbn.grid(column=x, row=y)
>                 self.buttons[-1].append(rbn)
>     def get_row_state(self, row):
>         return tuple(row == var.get() for var in self.variables)
> 
> if __name__ == "__main__":
>     root = tk.Tk()
>     def show_state(*args):
>         for i in range(3):
>             print "row", i, rg.get_row_state(i)
>         print
>     rg = Radiogrid(root,
>      ["alpha beta gamma".split(),
>      "one two three".split(),
>      "guido van rossum".split()],
>      show_state
>     )
>     rg.pack()
>     root.mainloop()
> 
> I hope this will move further discussion from the abstract to the
> concrete :-)
> 
> Peter
> 



More information about the Python-list mailing list