Radio button

Use radio buttons when you have a group of mutually exclusive choices and only one selection from the group is allowed.

Overview

../../_images/radio-overview.png

Radio buttons are used for mutually exclusive choices, not for multiple choices. Only one radio button can be selected at a time. When a user chooses a new item, the previous choice is automatically deselected.

Live demo

Note

This live demo contains only a preview of functionality and styles available for this component. Actual widgets may not show the exact same behavior but similar to expected.

CScreen:

    CRadioGroupLayout:
        pos_hint: {"center_x": 0.5, "center_y": 0.5}

        CRadioGroupLabel:
            text: "Radio button group"

        CRadioGroupHelperText:
            text: "Helper text"

        CRadioGroup:
            id: group_master
            pos_hint: {"center_x": 0.5, "center_y": 0.5}
            size_hint_x: None
            width: dp(256)

            CRadioItem:

                CRadioButton:
                    pos_hint: {"center_y": 0.5}
                    group_master: group_master

                CRadioButtonLabel:
                    text: "Radio button label"
                    pos_hint: {"center_y": 0.5}

            CRadioItem:

                CRadioButton:
                    pos_hint: {"center_y": 0.5}
                    group_master: group_master

                CRadioButtonLabel:
                    text: "Radio button label"
                    pos_hint: {"center_y": 0.5}

            CRadioItem:

                CRadioButton:
                    pos_hint: {"center_y": 0.5}
                    group_master: group_master

                CRadioButtonLabel:
                    text: "Radio button label"
                    pos_hint: {"center_y": 0.5}

Example

import os
import sys

from kivy.resources import resource_add_path

sys.path.insert(0, os.path.dirname(__file__))
resource_add_path(os.path.dirname(__file__))

from kivy.clock import Clock
from kivy.core.window import Window

from webview import WebView, prewarm_webview


def set_softinput(*args) -> None:
    Window.keyboard_anim_args = {"d": 0.2, "t": "in_out_expo"}
    Window.softinput_mode = "below_target"


Window.on_restore(Clock.schedule_once(set_softinput, 0.1))

appkv = """
CScreen:

    CRadioGroupLayout:
        pos_hint: {"center_x": 0.5, "center_y": 0.5}

        CRadioGroupLabel:
            text: "Radio button group"

        CRadioGroupHelperText:
            text: "Helper text"

        CRadioGroup:
            id: group_master
            pos_hint: {"center_x": 0.5, "center_y": 0.5}
            size_hint_x: None
            width: dp(256)

            CRadioItem:

                CRadioButton:
                    pos_hint: {"center_y": 0.5}
                    group_master: group_master

                CRadioButtonLabel:
                    text: "Radio button label"
                    pos_hint: {"center_y": 0.5}

            CRadioItem:

                CRadioButton:
                    pos_hint: {"center_y": 0.5}
                    group_master: group_master

                CRadioButtonLabel:
                    text: "Radio button label"
                    pos_hint: {"center_y": 0.5}

            CRadioItem:

                CRadioButton:
                    pos_hint: {"center_y": 0.5}
                    group_master: group_master

                CRadioButtonLabel:
                    text: "Radio button label"
                    pos_hint: {"center_y": 0.5}
"""

from kivy.lang import Builder

from carbonkivy.app import CarbonApp
from carbonkivy.uix.screen import CScreen


class myapp(CarbonApp):
    def __init__(self, *args, **kwargs):
        super(myapp, self).__init__(*args, **kwargs)

    def build(self) -> CScreen:
        screen = Builder.load_string(appkv)
        return screen


if __name__ == "__main__":
    myapp().run()

API

class carbonkivy.uix.radio.radio.CRadioButton(*args: Any, **kwargs: Any)[source]

Bases: CIconCircular, StateFocusBehavior, ButtonBehavior, SelectableBehavior

CRadioButton is a custom checkbox widget that inherits from AdaptiveBehavior, CIconCircular, BackgroundColorBehaviorCircular, StateFocusBehavior and ButtonBehavior.

active[source]

BooleanProperty(defaultvalue=True, **kw) Property that represents only a boolean value.

Parameters:
defaultvalue: boolean

Specifies the default value of the property.

group_master[source]

ObjectProperty(defaultvalue=None, rebind=False, **kw) Property that represents a Python object.

Parameters:
defaultvalue: object type

Specifies the default value of the property.

rebind: bool, defaults to False

Whether kv rules using this object as an intermediate attribute in a kv rule, will update the bound property when this object changes.

That is the standard behavior is that if there’s a kv rule text: self.a.b.c.d, where a, b, and c are properties with rebind False and d is a StringProperty. Then when the rule is applied, text becomes bound only to d. If a, b, or c change, text still remains bound to d. Furthermore, if any of them were None when the rule was initially evaluated, e.g. b was None; then text is bound to b and will not become bound to d even when b is changed to not be None.

By setting rebind to True, however, the rule will be re-evaluated and all the properties rebound when that intermediate property changes. E.g. in the example above, whenever b changes or becomes not None if it was None before, text is evaluated again and becomes rebound to d. The overall result is that text is now bound to all the properties among a, b, or c that have rebind set to True.

**kwargs: a list of keyword arguments
baseclass

If kwargs includes a baseclass argument, this value will be used for validation: isinstance(value, kwargs[‘baseclass’]).

Warning

To mark the property as changed, you must reassign a new python object.

Changed in version 1.9.0: rebind has been introduced.

Changed in version 1.7.0: baseclass parameter added.

on_group_master(*args)[source]
on_touch_down(touch: kivy.input.providers.mouse.MouseMotionEvent) bool[source]
class carbonkivy.uix.radio.radio.CRadioButtonLabel(*args: Any, **kwargs: Any)[source]

Bases: CLabel

class carbonkivy.uix.radio.radio.CRadioGroup(**kwargs)[source]

Bases: CSelectionLayout

on_parent(*args) None[source]
class carbonkivy.uix.radio.radio.CRadioGroupHelperText(*args: Any, **kwargs: Any)[source]

Bases: CLabel

class carbonkivy.uix.radio.radio.CRadioGroupLabel(*args: Any, **kwargs: Any)[source]

Bases: CLabel

class carbonkivy.uix.radio.radio.CRadioGroupLayout(*args: Any, **kwargs: Any)[source]

Bases: RelativeLayout

class carbonkivy.uix.radio.radio.CRadioItem(*args: Any, **kwargs: Any)[source]

Bases: CBoxLayout

class carbonkivy.uix.radio.radio.CRadioItemLabelNeutral(*args: Any, **kwargs: Any)[source]

Bases: CLabelNeutral