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 StateFocusBehavior class.
Usage#
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.behaviors import CommonElevationBehavior
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.behaviors.focus_behavior import StateFocusBehavior
KV = '''
MDScreen:
md_bg_color: app.theme_cls.backgroundColor
FocusWidget:
size_hint: .5, .3
pos_hint: {"center_x": .5, "center_y": .5}
md_bg_color: self.theme_cls.surfaceContainerHighestColor
MDLabel:
text: "Label"
pos_hint: {"center_y": .5}
halign: "center"
'''
class FocusWidget(MDBoxLayout, CommonElevationBehavior, StateFocusBehavior):
def on_enter(self):
'''Fired when mouse enter the bbox of the widget.'''
self.md_bg_color = self.theme_cls.surfaceVariantColor
def on_leave(self):
'''Fired when the mouse goes outside the widget border.'''
self.md_bg_color = self.theme_cls.surfaceContainerHighestColor
class Exmple(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
return Builder.load_string(KV)
Exmple().run()
from kivymd.app import MDApp
from kivymd.uix.behaviors import CommonElevationBehavior
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.behaviors.focus_behavior import StateFocusBehavior
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import MDScreen
class FocusWidget(MDBoxLayout, CommonElevationBehavior, StateFocusBehavior):
def on_enter(self):
'''Fired when mouse enter the bbox of the widget.'''
self.md_bg_color = self.theme_cls.surfaceVariantColor
def on_leave(self):
'''Fired when the mouse goes outside the widget border.'''
self.md_bg_color = self.theme_cls.surfaceContainerHighestColor
class Exmple(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
return (
MDScreen(
FocusWidget(
MDLabel(
text="Label",
pos_hint={"center_y": .5},
halign="center",
),
size_hint=(.5, .3),
pos_hint={"center_x": .5, "center_y": .5},
md_bg_color=self.theme_cls.surfaceContainerHighestColor,
),
md_bg_color=self.theme_cls.backgroundColor,
)
)
Exmple().run()
Color change at focus/defocus
FocusWidget:
focus_color: 1, 0, 1, 1
unfocus_color: 0, 0, 1, 1
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.StateFocusBehavior(*args, **kwargs)#
Focus behavior class.
- Events:
on_enterFired when mouse enters the bbox of the widget AND the widget is visible.
on_leaveFired when the mouse exits the widget AND the widget is visible.
For more information, see in the
HoverBehaviorclass documentation.Added in version 2.0.0.
- focus_behavior#
Using focus when hovering over a widget.
focus_behavioris aBooleanPropertyand defaults to False.
- focus_color#
The color of the widget when the mouse enters the bbox of the widget.
focus_coloris aColorPropertyand defaults to None.
- unfocus_color#
The color of the widget when the mouse exits the bbox widget.
unfocus_coloris aColorPropertyand defaults to None.
- class kivymd.uix.behaviors.focus_behavior.FocusBehavior(*args, **kwargs)#
Focus behavior class.
For more information, see in the
StateFocusBehaviorclass documentation.Deprecated since version 2.0.0.