Focus#
Changing the background color when the mouse is on the widget. To apply focus behavior, you must create a new class that is inherited from the
widget to which you apply the behavior and from the Color change at focus/defocusFocusBehavior
class.Usage#
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.behaviors import RectangularElevationBehavior
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.behaviors.focus_behavior import FocusBehavior
KV = '''
MDScreen:
md_bg_color: 1, 1, 1, 1
FocusWidget:
size_hint: .5, .3
pos_hint: {"center_x": .5, "center_y": .5}
md_bg_color: app.theme_cls.bg_light
MDLabel:
text: "Label"
theme_text_color: "Primary"
pos_hint: {"center_y": .5}
halign: "center"
'''
class FocusWidget(MDBoxLayout, RectangularElevationBehavior, FocusBehavior):
pass
class Test(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
return Builder.load_string(KV)
Test().run()
FocusWidget:
focus_color: 1, 0, 1, 1
unfocus_color: 0, 0, 1, 1
API - kivymd.uix.behaviors.focus_behavior
#
- class kivymd.uix.behaviors.focus_behavior.FocusBehavior(**kwargs)#
Focus behavior class.
For more information, see in the
HoverBehavior
andButtonBehavior
classes documentation.- Events:
- focus_behavior#
Using focus when hovering over a widget.
focus_behavior
is aBooleanProperty
and defaults to False.
- focus_color#
The color of the widget when the mouse enters the bbox of the widget.
focus_color
is aColorProperty
and defaults to None.
- unfocus_color#
The color of the widget when the mouse exits the bbox widget.
unfocus_color
is aColorProperty
and defaults to None.
- on_enter(self)#
Called when mouse enter the bbox of the widget.
- on_leave(self)#
Called when the mouse exit the widget.