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 MDFlatButton

KV = '''
MDScreen:

    MDBoxLayout:
        adaptive_size: True
        spacing: "12dp"
        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(MDFlatButton, MDToggleButton):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.background_down = self.theme_cls.primary_color


class Test(MDApp):
    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Orange"
        return Builder.load_string(KV)


Test().run()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/toggle-button-1.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.MDToggleButtonBehavior(**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].