Radio button bar: get selected button for a group radio button : RadioButton « GUI Tk « Python






Radio button bar: get selected button for a group radio button

Radio button bar: get selected button for a group radio button

from Tkinter import *

class Radiobar(Frame):
    def __init__(self, parent=None, picks=[], side=LEFT, anchor=W):
        Frame.__init__(self, parent)
        self.var = StringVar()
        for pick in picks:
            rad = Radiobutton(self, text=pick, value=pick, variable=self.var)
            rad.pack(side=side, anchor=anchor, expand=YES)
    def state(self):
        return self.var.get()


class Quitter(Frame):                      
    def __init__(self, parent=None):         
        Frame.__init__(self, parent)
        self.pack()
        widget = Button(self, text='Quit', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('Title', "Really quit?")
        if ans: Frame.quit(self)


if __name__ == '__main__':
    root = Tk()
    gui = Radiobar(root, ['A', 'B', 'C'], side=TOP, anchor=NW)
    gui.pack(side=LEFT, fill=Y)
    gui.config(relief=RIDGE,  bd=2)

    def allstates(): print gui.state() 
    Quitter(root).pack(side=RIGHT)
    Button(root, text='Peek', command=allstates).pack(side=RIGHT)
    root.mainloop()

           
       








Related examples in the same category

1.Radio Button DemoRadio Button Demo
2.Toogle group Radio ButtonToogle group Radio Button
3.Radio button in a groupRadio button in a group
4.Radio Button MenuRadio Button Menu
5.Get Radio button stateGet Radio button state
6.Hold on to your radio variables Hold on to your radio variables
7.Save Radio button statesSave Radio button states
8.What happens when some buttons have same valueWhat happens when some buttons have same value