tkinter radiobutton
William Gill
noreply at gcgroup.net
Tue Jun 28 17:54:31 EDT 2005
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.
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