Almost all Perl/Tk applications use Buttons in one way or another. There are three different types of Button widgets available in the standard Perl/Tk set: Button, Checkbutton, and Radiobutton. This chapter covers all three types of Buttons and includes examples of where you might use one over the other.
Each of the Buttons we cover in this chapter look different, primarily in their use of selection indicators. The Button widget doesn’t use indicators at all, but Checkbutton and Radiobutton widgets use them to indicate whether the Button has been selected or not. The Button widgets are:
A plain Button, shown in Figure 4-1. The user can press it and usually an immediate action results.
A Checkbutton, shown checked in Figure 4-2. When checked or unchecked, only the visual representation is changed; the state is not validated until later in the program. Checkbuttons can be used singly or in groups.
A Radiobutton, shown unchecked in Figure 4-3. When checked or unchecked, only the visual representation is changed; the state is not validated until later in the program. Radiobuttons are always used in groups of two or more.
A Button is one of the simplest Perl/Tk widgets: the user presses it and something immediately happens. The label of the Button should make the action clear; for example, text such as Quit, Save, or Print gives the user a good idea of what will happen when she clicks the Button. After the Button has been clicked, it will look exactly the same as before, unless programmed to change text or color.
Checkbuttons are for when you want to select none, some, or all items. For example, a shopping list might consist of Bread, Milk, Soda, and Eggs. Select none if you don’t need any of them, or select all if you’re out of everything. As the user clicks on each Checkbutton, the selection indicator will be filled in or left blank as appropriate (see Figure 4-4).
After a user presses the Purchase Button, the code examines the value of each Checkbutton’s variable to decide what tasks to perform next.
Radiobuttons, on the other hand, all assign the same variable and are necessarily related. Radiobuttons are used in situations when you must make a choice between items, such as on a multiple-choice exam, as shown in Figure 4-5.
Because each Radiobutton in a group is associated with the same variable, you are forced to select one and only one choice in that group. If the default choice is always E and you click on D, E is unselected automatically.