ToggleButton

This behavior must always be inherited after the button’s Widget class since it works with the inherited properties of the button class.

example:

class MyToggleButtonWidget(MDFlatButton, MDToggleButton):
    # [...]
    pass
from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.uix.behaviors.toggle_behavior import MDToggleButton
from kivymd.uix.button import MDRectangleFlatButton

KV = '''
Screen:

    MDBoxLayout:
        adaptive_size: True
        pos_hint: {"center_x": .5, "center_y": .5}

        MyToggleButton:
            text: "Show ads"
            group: "x"

        MyToggleButton:
            text: "Do not show ads"
            group: "x"

        MyToggleButton:
            text: "Does not matter"
            group: "x"
'''


class MyToggleButton(MDRectangleFlatButton, MDToggleButton):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.background_down = self.theme_cls.primary_light


class Test(MDApp):
    def build(self):
        return Builder.load_string(KV)


Test().run()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/toggle-button-1.gif
class MyToggleButton(MDFillRoundFlatButton, MDToggleButton):
    def __init__(self, **kwargs):
    self.background_down = MDApp.get_running_app().theme_cls.primary_dark
    super().__init__(**kwargs)
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/toggle-button-2.gif

You can inherit the MyToggleButton class only from the following classes

  • MDRaisedButton

  • MDFlatButton

  • MDRectangleFlatButton

  • MDRectangleFlatIconButton

  • MDRoundFlatButton

  • MDRoundFlatIconButton

  • MDFillRoundFlatButton

  • MDFillRoundFlatIconButton

API - kivymd.uix.behaviors.toggle_behavior

class kivymd.uix.behaviors.toggle_behavior.MDToggleButton(**kwargs)

This mixin class provides togglebutton behavior. Please see the togglebutton behaviors module documentation for more information.

New in version 1.8.0.

background_normal

Color of the button in rgba format for the ‘normal’ state.

background_normal is a ColorProperty and is defaults to None.

background_down

Color of the button in rgba format for the ‘down’ state.

background_down is a ColorProperty and is defaults to None.

font_color_normal

Color of the font’s button in rgba format for the ‘normal’ state.

font_color_normal is a ColorProperty and is defaults to None.

font_color_down

Color of the font’s button in rgba format for the ‘down’ state.

font_color_down is a ColorProperty and is defaults to [1, 1, 1, 1].