Magic#
#
Magical effects for buttons.
Warning
Magic effects do not work correctly with KivyMD buttons!
To apply magic effects, you must create a new class that is inherited from the
widget to which you apply the effect and from the MagicBehavior class.
class MagicButton(MagicBehavior, MDButton):
...
The MagicBehavior class provides five effects:
Example#
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivymd.app import MDApp
from kivymd.uix.behaviors import MagicBehavior
from kivymd.uix.button import MDButton, MDButtonText
KV = '''
MDScreen:
md_bg_color: app.theme_cls.backgroundColor
MagicButtonGrowEffect:
on_release: self.grow()
pos_hint: {"center_x": .5, "center_y": .4}
MagicButtonShakeEffect:
on_release: self.shake()
pos_hint: {"center_x": .5, "center_y": .5}
MagicButtonTwistEffect:
on_release: self.twist()
pos_hint: {"center_x": .5, "center_y": .6}
MagicButtonShrinkEffect:
on_release: self.shrink()
pos_hint: {"center_x": .5, "center_y": .7}
MagicButtonWobbleEffect:
on_release: self.wobble()
pos_hint: {"center_x": .5, "center_y": .8}
'''
class BaseMagicButton(MDButton):
'''
A base button class with customizable text and outlined style.
This class serves as a foundation for creating magic-effect buttons
(like grow, shake, twist) with predefined styling and structure.
It automatically initializes a button with MDButtonText as its child widget.
'''
text = StringProperty()
style = "outlined"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.widgets = [
MDButtonText(
text=self.text
)
]
super().__init__(*args, **kwargs)
class MagicButtonGrowEffect(MagicBehavior, BaseMagicButton):
scale_value = 1.03
text = "Grow Effect"
class MagicButtonShakeEffect(MagicBehavior, BaseMagicButton):
translate_value = 15
text = "Shake Effect"
class MagicButtonTwistEffect(MagicBehavior, BaseMagicButton):
rotate_value = 6
text = "Twist Effect"
class MagicButtonShrinkEffect(MagicBehavior, BaseMagicButton):
scale_value = 0.95
text = "Shrink Effect"
class MagicButtonWobbleEffect(MagicBehavior, BaseMagicButton):
scale_value = 0.95
text = "Wobble Effect"
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
Example().run()
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivymd.app import MDApp
from kivymd.uix.behaviors import MagicBehavior
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.screen import MDScreen
class BaseMagicButton(MDButton):
'''
A base button class with customizable text and outlined style.
This class serves as a foundation for creating magic-effect buttons
(like grow, shake, twist) with predefined styling and structure.
It automatically initializes a button with MDButtonText as its child widget.
'''
text = StringProperty()
style = "outlined"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Clock.schedule_once(self.add_text)
def add_text(self, *args) -> None:
self.widgets = [
MDButtonText(
text=self.text,
pos_hint={"center_x": .5, "center_y": .5}
)
]
def on_release(self, *args) -> None:
super().on_release(args)
{
"Grow": self.grow,
"Shake": self.shake,
"Twist": self.twist,
"Shrink": self.shrink,
"Wobble": self.wobble,
}.get(self.text.split()[0], "Grow")()
class MagicButtonGrowEffect(MagicBehavior, BaseMagicButton):
scale_value = 1.03
text = "Grow Effect"
class MagicButtonShakeEffect(MagicBehavior, BaseMagicButton):
translate_value = 15
text = "Shake Effect"
class MagicButtonTwistEffect(MagicBehavior, BaseMagicButton):
rotate_value = 6
text = "Twist Effect"
class MagicButtonShrinkEffect(MagicBehavior, BaseMagicButton):
scale_value = 0.95
text = "Shrink Effect"
class MagicButtonWobbleEffect(MagicBehavior, BaseMagicButton):
scale_value = 0.95
text = "Wobble Effect"
class Example(MDApp):
def build(self):
return (
MDScreen(
MDBoxLayout(
MagicButtonGrowEffect(
),
MagicButtonShakeEffect(
),
MagicButtonTwistEffect(
),
MagicButtonShrinkEffect(
),
MagicButtonWobbleEffect(
),
spacing="24dp",
orientation="vertical",
adaptive_size=True,
pos_hint={"center_x": .5, "center_y": .5}
),
md_bg_color=self.theme_cls.backgroundColor,
)
)
Example().run()
API - kivymd.uix.behaviors.magic_behavior#
- class kivymd.uix.behaviors.magic_behavior.MagicBehavior#
A mixin class that provides animated visual effects for Kivy/KivyMD widgets.
MagicBehavior adds interactive animation effects that enhance user interface feedback and engagement. These animations include:
grow(): Expands the widget slightly and returns to its original size.
shake(): Shakes the widget horizontally.
wobble(): Squashes and stretches the widget briefly.
twist(): Rotates the widget and resets its angle.
shrink(): Shrinks the widget temporarily and restores it.
- scale_value#
Scale factor for animation effects.
Added in version 2.0.0.
scale_valueis aNumericPropertyand defaults to 1.
- translate_value#
Translation distance for animation effects.
Added in version 2.0.0.
translate_valueis aNumericPropertyand defaults to 1.
- rotate_value#
Rotation angle in degrees for animation effects.
Added in version 2.0.0.
rotate_valueis aNumericPropertyand defaults to 25.
- magic_speed#
Animation playback speed.
magic_speedis aNumericPropertyand defaults to 1.
- on_touch_up(*args)#