Creating radio buttons from a list in python?
Question: The code below creates a check button for each item in the list:

    
    
    cb_strings = ['item 1', 'item 2', 'item 3', 'item 4']
    
    self.check_btns = []
    
    for i in range(len(cb_strings)):
        v = StringVar()
        self.check_btns.append(Checkbutton(parent, width = 20, variable = v, anchor = W, onvalue = cb_strings[i], offvalue = '*', text = cb_strings[i] , command = self.display_selections))
        self.check_btns[i].var = v
        self.check_btns[i].deselect()
        self.check_btns[i].pack()
    

Could you show me how to do a similar thing, but to create radio buttons
instead?

Thanks in advance :)


Answer: Here's an example:

    
    
    from Tkinter import *
    
    cb_strings = ['item 1', 'item 2', 'item 3', 'item 4']
    
    def sel():
       print "You selected the option " + str(var.get())
    
    root = Tk()
    var = StringVar()
    var.set(cb_strings[0])
    
    for item in cb_strings:
        button = Radiobutton(root, text=item, variable=var, value=item, command=sel)
        button.pack(anchor=W)
    
    root.mainloop()
    

Also see [example from "An Introduction to
Tkinter"](http://effbot.org/tkinterbook/radiobutton.htm).

