tkinter radiobutton

Peter Otten __peter__ at web.de
Tue Jun 28 15:45:52 EDT 2005


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