The Radio Button Group Widget
-
Introduction
A radio button is also called an option button. It is actually a group of buttons, and only one of the buttons in a group can be selected. If you select any button, then the previously selected button in this group will become unselected. Radio buttons are most useful when you need to give the user a few choices, but only want the user to pick one of them.
To add a group of radio buttons, use the following block:
Parameters:- The 6 inputs allow us to specify up to 6 different choices. If you only want to offer 3 options, then fill in the first 3 input fields.
- The radio buttons can be arranged vertically or horizontaly.
- You can specify the position and size of the radio button group
- Lastly, you can give this group a name.
For example, the following program adds 2 groups of radio buttons. These 2 groups are independent of each other.
Set Value To a Radio Button Group
To make sure one of the radio buttons is selected by default, you can use the “set value to widget” block. You have to use one of the options as the new value:
The above program will produce this:
Handle Value Changes
To make your program “reactive”, you can run some code whenever the value of the radio button group changes. To do that, you need to use the “when widget changes” block to handle the change event, and use the “value of” block to read the currently selected option of the group.
For example, the following program sets a different color effect based on the user’s selection:
Similarly, the following program changes the sprite’s direction based on the second radio button group: