tkinter radiobutton

Peter Otten __peter__ at web.de
Mon Jun 27 18:57:32 CEST 2005


William Gill wrote:

> I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep
> references to them in a 2 dimensional list ( rBtns[r][c] ).  It works
> fine, and I can even make it so only one button per column can be
> selected, by assigning each column to an intVar.  In many languages a
> radiobutton has a property that can be directly read to see if it is
> selected on unselected.  Tkinter radiobuttons don't seem to have any
> such property.  Is there any way to look (via the script not the screen)
> to determine if it is selected?, or can this only be achieved via
> control variables?

You can either write a little helper function

def selected(rbn):
    return rbn.getvar(rbn["variable"]) == rbn["value"]

or use a custom subclass of Tkinter.Radiobutton with a 'selected' attribute:

class Radiobutton(Tkinter.Radiobutton):
    def __getattr__(self, name):
        if name == "selected":
            return self.getvar(self["variable"]) == self["value"]
        raise AttributeError


Peter




More information about the Python-list mailing list