SelectionControls#
#
Selection controls allow the user to select options.
KivyMD provides the following selection controls classes for use:
MDCheckbox#
Usage#
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDFloatLayout:
MDCheckbox:
size_hint: None, None
size: "48dp", "48dp"
pos_hint: {'center_x': .5, 'center_y': .5}
'''
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
self.theme_cls.theme_style = "Dark"
return Builder.load_string(KV)
Example().run()
from kivymd.app import MDApp
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.selectioncontrol import MDCheckbox
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
self.theme_cls.theme_style = "Dark"
return (
MDFloatLayout(
MDCheckbox(
size_hint=(None, None),
size=("48dp", "48dp"),
pos_hint={'center_x': .5, 'center_y': .5},
)
)
)
Example().run()
Note
Be sure to specify the size of the checkbox. By default, it is (dp(48), dp(48)), but the ripple effect takes up all the available space.
Control state#
MDCheckbox:
on_active: app.on_checkbox_active(*args)
MDCheckbox(
on_active=self.on_checkbox_active,
)
def on_checkbox_active(self, checkbox, value):
if value:
print('The checkbox', checkbox, 'is active', 'and', checkbox.state, 'state')
else:
print('The checkbox', checkbox, 'is inactive', 'and', checkbox.state, 'state')
MDCheckbox with group#
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
<Check@MDCheckbox>:
group: 'group'
size_hint: None, None
size: dp(48), dp(48)
MDFloatLayout:
Check:
active: True
pos_hint: {'center_x': .4, 'center_y': .5}
Check:
pos_hint: {'center_x': .6, 'center_y': .5}
'''
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
self.theme_cls.theme_style = "Dark"
return Builder.load_string(KV)
Example().run()
from kivymd.material_resources import dp
from kivymd.app import MDApp
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.selectioncontrol import MDCheckbox
class Check(MDCheckbox):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.group = 'group'
self.size_hint = (None, None)
self.size = (dp(48), dp(48))
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
self.theme_cls.theme_style = "Dark"
return (
MDFloatLayout(
Check(
pos_hint={'center_x': 0.4, 'center_y': 0.5},
),
Check(
pos_hint={'center_x': 0.6, 'center_y': 0.5},
)
)
)
Example().run()
Parent and child checkboxes#
Checkboxes can have a parent-child relationship with other checkboxes. When the parent checkbox is checked, all child checkboxes are checked. If a parent checkbox is unchecked, all child checkboxes are unchecked. If some, but not all, child checkboxes are checked, the parent checkbox becomes an indeterminate checkbox.
Usage#
MDCheckbox:
group: "root" # this is a required name for the parent checkbox group
MDCheckbox:
group: "child" # this is a required name for a group of child checkboxes
MDCheckbox:
group: "child" # this is a required name for a group of child checkboxes
MDCheckbox(
# This is a required name for the parent checkbox group.
group="root",
)
MDCheckbox(
# This is a required name for a group of child checkboxes.
group="child",
)
MDCheckbox(
# This is a required name for a group of child checkboxes.
group="child",
)
Example#
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
KV = '''
<CheckItem>
adaptive_height: True
MDCheckbox:
group: root.group
MDLabel:
text: root.text
adaptive_height: True
padding_x: "12dp"
pos_hint: {"center_y": .5}
MDBoxLayout:
orientation: "vertical"
md_bg_color: self.theme_cls.backgroundColor
MDBoxLayout:
orientation: "vertical"
adaptive_height: True
padding: "12dp", "36dp", 0, 0
spacing: "12dp"
CheckItem:
text: "Recieve emails"
group: "root"
MDBoxLayout:
orientation: "vertical"
adaptive_height: True
padding: "24dp", 0, 0, 0
spacing: "12dp"
CheckItem:
text: "Daily"
group: "child"
CheckItem:
text: "Weekly"
group: "child"
CheckItem:
text: "Monthly"
group: "child"
MDWidget:
'''
class CheckItem(MDBoxLayout):
text = StringProperty()
group = StringProperty()
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Teal"
return Builder.load_string(KV)
Example().run()
from kivy.properties import StringProperty
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.selectioncontrol import MDCheckbox
from kivymd.uix.widget import MDWidget
class CheckItem(MDBoxLayout):
text = StringProperty()
group = StringProperty()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.adaptive_height = True
self.widgets = [
MDCheckbox(
group=self.group,
),
MDLabel(
text=self.text,
adaptive_height=True,
padding_x="12dp",
pos_hint={"center_y": .5},
),
]
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Teal"
return (
MDBoxLayout(
MDBoxLayout(
CheckItem(
text="Recieve emails",
group="root",
),
MDBoxLayout(
CheckItem(
text="Daily",
group="child",
),
CheckItem(
text="Weekly",
group="child",
),
CheckItem(
text="Monthly",
group="child",
),
orientation="vertical",
adaptive_height=True,
padding=("24dp", 0, 0, 0),
spacing="12dp",
),
orientation="vertical",
adaptive_height=True,
padding=("12dp", "36dp", 0, 0),
spacing="12dp",
),
MDWidget(),
orientation="vertical",
md_bg_color=self.theme_cls.backgroundColor,
)
)
Example().run()
MDSwitch#
Usage#
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDFloatLayout:
MDSwitch:
pos_hint: {'center_x': .5, 'center_y': .5}
'''
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
self.theme_cls.theme_style = "Dark"
return Builder.load_string(KV)
Example().run()
from kivymd.app import MDApp
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.selectioncontrol import MDSwitch
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
self.theme_cls.theme_style = "Dark"
return (
MDFloatLayout(
MDSwitch(
pos_hint={'center_x': .5, 'center_y': .5},
)
)
)
Example().run()
Note
Control state of MDSwitch same way as in
MDCheckbox.
API - kivymd.uix.selectioncontrol.selectioncontrol#
- class kivymd.uix.selectioncontrol.selectioncontrol.MDCheckbox(**kwargs)#
Checkbox class.
For more information, see in the
StateLayerBehaviorandCircularRippleBehaviorandScaleBehaviorandToggleButtonBehaviorandMDIconclasses documentation.- checkbox_icon_normal#
Background icon of the checkbox used for the default graphical representation when the checkbox is not pressed.
checkbox_icon_normalis aStringPropertyand defaults to ‘checkbox-blank-outline’.
- checkbox_icon_down#
Background icon of the checkbox used for the default graphical representation when the checkbox is pressed.
checkbox_icon_downis aStringPropertyand defaults to ‘checkbox-marked’.
- radio_icon_normal#
Background icon (when using the group option) of the checkbox used for the default graphical representation when the checkbox is not pressed.
radio_icon_normalis aStringPropertyand defaults to ‘checkbox-blank-circle-outline’.
- radio_icon_down#
Background icon (when using the group option) of the checkbox used for the default graphical representation when the checkbox is pressed.
radio_icon_downis aStringPropertyand defaults to ‘checkbox-marked-circle’.
- color_active#
Color in (r, g, b, a) or string format when the checkbox is in the active state.
Added in version 1.0.0.
MDCheckbox: color_active: "red"
color_activeis aColorPropertyand defaults to None.
- color_inactive#
Color in (r, g, b, a) or string format when the checkbox is in the inactive state.
Added in version 1.0.0.
MDCheckbox: color_inactive: "blue"
color_inactiveis aColorPropertyand defaults to None.
- color_disabled#
Color in (r, g, b, a) or string format when the checkbox is in the disabled state.
Added in version 2.0.0: Use
color_disabledinstead.color_disabledis aColorPropertyand defaults to None.
- disabled_color#
Color in (r, g, b, a) or string format when the checkbox is in the disabled state.
Deprecated since version 2.0.0: Use
color_disabledinstead.disabled_coloris aColorPropertyand defaults to None.
- selected_color#
Color in (r, g, b, a) or string format when the checkbox is in the active state.
Deprecated since version 1.0.0: Use
color_activeinstead.selected_coloris aColorPropertyand defaults to None.
- unselected_color#
Color in (r, g, b, a) or string format when the checkbox is in the inactive state.
Deprecated since version 1.0.0: Use
color_inactiveinstead.unselected_coloris aColorPropertyand defaults to None.
- update_icon(*args) None#
Fired when the values of
checkbox_icon_normalandcheckbox_icon_downandradio_icon_normalandgroupchange.
- on_touch_down(touch)#
Receive a touch down event.
- Parameters:
- touch:
MotionEventclass Touch received. The touch is in parent coordinates. See
relativelayoutfor a discussion on coordinate systems.
- touch:
- Returns:
bool If True, the dispatching of the touch event will stop. If False, the event will continue to be dispatched to the rest of the widget tree.
- class kivymd.uix.selectioncontrol.selectioncontrol.MDSwitch(**kwargs)#
Switch class.
For more information, see in the
StateLayerBehaviorandButtonBehaviorandMDFloatLayoutclasses documentation.- width#
- height#
- md_bg_color_disabled#
The background color in (r, g, b, a) or string format of the switch when the switch is disabled.
md_bg_color_disabledis aColorPropertyand defaults to None.
- ripple_effect#
Allows or does not allow the ripple effect when activating/deactivating the switch.
Added in version 2.0.0.
ripple_effectis aBooleanPropertyand defaults to True.
- icon_active#
Thumb icon when the switch is in the active state (only M3 style).
Added in version 1.0.0.
MDSwitch: active: True icon_active: "check"
icon_activeis aStringPropertyand defaults to ‘’.
- icon_inactive#
Thumb icon when the switch is in an inactive state (only M3 style).
Added in version 1.0.0.
MDSwitch: icon_inactive: "close"
icon_inactiveis aStringPropertyand defaults to ‘’.
- icon_active_color#
Thumb icon color in (r, g, b, a) or string format when the switch is in the active state (only M3 style).
Added in version 1.0.0.
MDSwitch: active: True icon_active: "check" icon_active_color: "white"
icon_active_coloris aColorPropertyand defaults to None.
- icon_inactive_color#
Thumb icon color in (r, g, b, a) or string format when the switch is in an inactive state (only M3 style).
Added in version 1.0.0.
MDSwitch: icon_inactive: "close" icon_inactive_color: "grey"
icon_inactive_coloris aColorPropertyand defaults to None.
- thumb_color_active#
The color in (r, g, b, a) or string format of the thumb when the switch is active.
Added in version 1.0.0.
MDSwitch: active: True thumb_color_active: "brown"
thumb_color_activeis anColorPropertyand default to None.
- thumb_color_inactive#
The color in (r, g, b, a) or string format of the thumb when the switch is inactive.
Added in version 1.0.0.
MDSwitch: thumb_color_inactive: "brown"
thumb_color_inactiveis anColorPropertyand default to None.
- thumb_color_disabled#
The color in (r, g, b, a) or string format of the thumb when the switch is in the disabled state.
MDSwitch: active: True thumb_color_disabled: "brown" disabled: True
thumb_color_disabledis anColorPropertyand default to None.
- track_color_active#
The color in (r, g, b, a) or string format of the track when the switch is active.
MDSwitch: active: True track_color_active: "red"
track_color_activeis anColorPropertyand default to None.
- track_color_inactive#
The color in (r, g, b, a) or string format of the track when the switch is inactive.
Added in version 1.0.0.
MDSwitch: track_color_inactive: "red"
track_color_inactiveis anColorPropertyand default to None.
- track_color_disabled#
The color in (r, g, b, a) or string format of the track when the switch is in the disabled state.
MDSwitch: track_color_disabled: "lightgrey" disabled: True
track_color_disabledis anColorPropertyand default to None.
- line_color_disabled#
The color of the outline in the disabled state
Added in version 2.0.0.
line_color_disabledis anColorPropertyand defaults to None.
- set_icon(instance_switch, icon_value: str) None#
Fired when the values of
icon_activeandicon_inactivechange.