[Tkinter-discuss] Radio Buttons

Peter Lacroix pjlacroix at hotmail.com
Thu May 11 13:07:27 CEST 2006


Hi all,

Let's say you had the following class below (MakeChoice) that was used to 
cast a vote. When the program is running, eventually the user will confirm 
choices made through some sort of "SUBMIT" button. When that button is 
clicked, we want the selected value (on a few sets of radio buttons) to be 
written to a file. What is the container of the currently selected value and 
how does one have that value be written to a file when a button is clicked?

I hope my question is clear.

Thanks!

---------------------

class MakeChoice(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        # Creates a string variable to bind the five radio buttons together
        self.current_selection = StringVar()

        # Choice 1
        self.choice1 = Radiobutton( self )
        self.choice1["text"]     = "Queen Elizabeth"
        self.choice1["variable"] = self.current_selection,
        self.choice1["value"]    = "Queen Elizabeth",
        self.choice1["command"]  = self.set_text
        self.choice1.grid( row = 2, column = 0, sticky = W )

        # Choice 2
        self.choice2 = Radiobutton( self )
        self.choice2["text"]     = "Madonna"
        self.choice2["variable"] = self.current_selection,
        self.choice2["value"]    = "Madonna",
        self.choice2["command"]  = self.set_text
        self.choice2.grid( row = 3, column = 0, sticky = W )

        # Choice 3
        self.choice3 = Radiobutton( self )
        self.choice3["text"]     = "Mother Theresa"
        self.choice3["variable"] = self.current_selection,
        self.choice3["value"]    = "Mother Theresa",
        self.choice3["command"]  = self.set_text
        self.choice3.grid( row = 4, column = 0, sticky = W )

	# Choice 4
        self.choice4 = Radiobutton( self )
        self.choice4["text"]     = "Marge Simpson"
        self.choice4["variable"] = self.current_selection,
        self.choice4["value"]    = "Marge Simpson",
        self.choice4["command"]  = self.set_text
        self.choice4.grid( row = 5, column = 0, sticky = W )

	# Choice 5
        self.choice5 = Radiobutton( self )
        self.choice5["text"]     = "Abstain"
        self.choice5["variable"] = self.current_selection,
        self.choice5["value"]    = "abstain. Your vote will be counted but 
will not reflect a particular choice.",
        self.choice5["command"]  = self.set_text
        self.choice5.grid( row = 6, column = 0, sticky = W )

        # Creates a text field to show the selected option
        self.selection_text = Text(self, width = 40, height = 3, wrap = 
WORD)
        self.selection_text.grid(row = 7, column = 0, columnspan = 3)

    def set_text( self ):
        text = "You selected "
        text += self.current_selection.get()
        self.selection_text.delete( 0.0, END )
        self.selection_text.insert( 0.0, text )

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! 
http://search.msn.click-url.com/go/onm00200636ave/direct/01/



More information about the Tkinter-discuss mailing list