pythonware.com products ::: library ::: search ::: daily Python-URL!
   

Patterns

Back   Next   

 Patterns

The Radiobutton widget is very similar to the check button. To get a proper radio behavior, make sure to have all buttons in a group point to the same variable, and use the value option to specify what value each button represents:

    v = IntVar()
    Radiobutton(master, text="One", variable=v, value=1).pack(anchor=W)
    Radiobutton(master, text="Two", variable=v, value=2).pack(anchor=W)

If you need to get notified when the value changes, attach a command callback to each button.

To create a large number of buttons, use a loop:

    MODES = [
        ("Monochrome", "1"),
        ("Grayscale", "L"),
        ("True color", "RGB"),
        ("Color separation", "CMYK"),
    ]
    
    v = StringVar()
    v.set("L") # initialize
    
    for text, mode in MODES:
        b = Radiobutton(master, text=text,
                        variable=v, value=mode)
        b.pack(anchor=W)

Figure 38-1. Standard radiobuttons

To turn the above example into a "button box" rather than a set of radio buttons, set the indicatoron option to 0. In this case, there's no separate radio button indicator, and the selected button is drawn as SUNKEN instead of RAISED:

Figure 38-2. Using indicatoron=0

Back   Next