Using Radio Buttons


Radio buttons are a lot like check buttons, except that radio buttons only allow one button in a group to be selected at once. This is great if you want the user to make a single selection from a group of choices. Since radio buttons have so much in common with check buttons, learning to use them is pretty straight-forward.

Introducing the Movie Chooser 2 Program

The Movie Chooser 2 program is like the Movie Chooser program. The user is presented with three different movie types from which to select. The difference is that the Movie Chooser 2 program uses radio buttons instead of check buttons so the user can select only one movie type. This is perfect since the program asks the user for his or her favorite type of movie. Figure 10.15 shows off the program.


Figure 10.15: The user can select only a single movie type.

Setting Up the Program

I start the program by importing the Tkinter module:

 # Movie Chooser 2 # Demonstrates radio buttons # Michael Dawson - 6/9/03 from Tkinter import * 

Next, I write the Application class. I define its constructor, which initializes a new Application object:

 class Application(Frame):     """ GUI Application for favorite movie type. """     def __init__(self, master):         """ Initialize Frame. """         Frame.__init__(self, master)         self.grid()         self.create_widgets() 

Then, I create labels that give the user instructions:

     def create_widgets(self):         """ Create widgets for movie type choices. """         # create description label         Label(self,                text = "Choose your favorite type of movie"                ).grid(row = 0, column = 0, sticky = W)         # create instruction label         Label(self,               text = "Select one:"               ).grid(row = 1, column = 0, sticky = W) 

Creating Radio Buttons

Since only one radio button in a group can be selected at one time, there's no need for each radio button to have its own status variable, as required for check buttons. Instead, a group of radio buttons share one, special object that reflects which of the radio buttons is selected. This object must be an instance of the StringVar class from the Tkinter module, which allows a string to be stored and retrieved. So, before I create the radio buttons themselves, I create a single String-Var object for all of the radio buttons to share and then I assign it to the attribute favorite:

         # create variable for single, favorite type of movie         self.favorite = StringVar() 

Next, I create the Comedy radio button:

         # create Comedy radio button         Radiobutton(self,                      text = "Comedy",                      variable = self.favorite,                      value = "comedy.",                      command = self.update_text                      ).grid(row = 2, column = 0, sticky = W) 

A radio button's variable option defines the StringVar associated with the radio button, while a radio button's value option defines the string to be stored by the StringVar when the radio button is selected. So, by setting this radio button's variable option to self.favorite and its value option to "comedy.", I'm saying that when the Comedy radio button is selected, the StringVar referenced by self.favorite should store the string "comedy."

Next, I create the other two radio buttons:

         # create Drama radio button         Radiobutton(self,                      text = "Drama",                      variable = self.favorite,                      value = "drama.",                      command = self.update_text                      ).grid(row = 3, column = 0, sticky = W)         # create Romance radio button         Radiobutton(self,                      text = "Romance",                      variable = self.favorite,                      value = "romance.",                      command = self.update_text                      ).grid(row = 4, column = 0, sticky = W) 

By setting the Drama radio button's variable option to self.favorite and its value option to "drama.", I'm saying that when the Drama radio button is selected, the StringVar referenced by self.favorite should store the string "drama."

And by setting the Romance radio button's variable option to self.favorite and its value option to "romance.", I'm saying that when the Romance radio button is selected, the StringVar referenced by self.favorite should store the string "romance."

Next, I create the text box to display the results of the user's selection:

         # create text field to display result         self.results_txt = Text(self, width = 40, height = 5, wrap = WORD)         self.results_txt.grid(row = 5, column = 0, columnspan = 3) 

Getting a Value from a Group of Radio Buttons

Getting a value from a group of radio buttons is as simple as invoking the get() method of the StringVar object that they all share:

     def update_text(self):         """ Update text area and display user's favorite movie type. """         message = "Your favorite type of movie is "         message += self.favorite.get() 

When the Comedy radio button is selected, self.favorite.get() returns "comedy."; when the Drama radio button is selected, self.favorite.get() returns "drama."; and when the Romance radio button is selected, self.favorite.get() returns "romance."

Next, I delete any text that may be in the text box and insert the string I just created, which declares the user's favorite movie type:

         self.results_txt.delete(0.0, END)         self.results_txt.insert(0.0, message) 

Wrapping Up the Program

I wrap up the program by creating a root window and a new Application object. Then, I begin the root window's event loop to start up the GUI.

 # main root = Tk() root.title("Movie Chooser 2") app = Application(root) root.mainloop() 




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net