Radio Buttons

Radio buttons provide a user with a way to select one of a number of labeled options displayed simultaneously in a window.  The next screen shot shows two program windows that allow the user to select his academic class.  When one button is selected, the other three are deselected.  When the user presses the Display button, the program determines which radio button is currently selected and displays its label in the text field.  The window on the left aligns the radio buttons vertically, whereas the window on the right aligns the buttons horizontally.


Vertical Alignment

Horizontal Alignment


Radio buttons belong to a radio button group.  The programmer first adds a radio button group to the window, and then adds each radio button to the group.  The alignment of buttons in a group is vertical by default, but can be overridden to horizontal when the group is added.  The radio buttons will appear in their group in the order in which they are added.  Here is the program ( that produces the second window shown above.


from breezypythongui import EasyFrame

from tkinter import HORIZONTAL


class RadioButtonDemo(EasyFrame):

    """When the Display button is pressed, shows the label

    of the selected radio button.  The button group has a

    horizontal alignment."""


    def __init__(self):

        """Sets up the window and widgets."""

        EasyFrame.__init__(self, "Radio Button Demo")


        # Add the button group = self.addRadioButtonGroup(row = 1, column = 0,

                                              columnspan = 4,

                                              orient = HORIZONTAL)


        # Add the radio buttons to the group"Freshman")"Sophomore")"Junior")

        defaultRB ="Senior")


        # Select one of the buttons in the group


        # Output field and command button for the results

        self.output = self.addTextField("", row = 0, column = 1)

        self.addButton("Display", row = 0, column = 0,

                       command = self.display)


    # Event handling method


    def display(self):

        """Displays the selected button's label in the text field."""



A radio button has two important attributes, named label (the string argument to the addRadioButton method) and value.  When a radio button is added to a group, these two attributes are given the same value.  When the user selects a radio button, the button group records that buttonÕs value attribute.  This enables the getSelectedButton method to return the groupÕs currently selected button to the application.  The setSelectedButton method enables the application to select a button under program control.  In the example program shown earlier, the value attribute of the selected button is sent to a text field for display.  While the two attributes can be examined and reset, an application really should only examine them.