Hover❖

Module path❖
kivydk.uix.behavior.hover

Overview❖

This behavior adds hover awareness to widgets, allowing them to react when a pointer enters or leaves their area. It provides an easy way to create interactive UI elements that respond visually or functionally to hover interactions, regardless of the input device.


Example❖

Sample illustrating how HoverBehavior can be used to create widgets that react to mouse hover.

../../../_images/TestHover.gif
 1#// IMPORT
 2from kivy.uix.button import Button
 3
 4from kivydk.uix.behavior import HoverBehavior
 5
 6
 7#// LOGIC
 8class TestHover(HoverBehavior, Button):
 9    """ Updates the Button's color and text to reflect the current hover state. """
10    def __init__(self, **kwargs):
11        super().__init__(**kwargs)
12
13        # Initialize the button style with default information
14        self.on_hover(self.hovered)
15
16    def on_hover(self, state):
17        """
18        Update the label text and background color to reflect the current hover state.
19
20        :param state: ```True`` when the widget is hovered and ``False`` otherwise.
21        """
22        self.background_color = [0.8, 0.4, 0.2, 1.0] if state else [1.0, 1.0, 1.0, 1.0]
23        self.text = "Hovered" if state else "Unhovered"
24
25
26#// RUN FILE
27if __name__ == "__main__":
28    from kivydk.app import App
29
30    class Example(App):
31        def build(self):
32            return TestHover(size_hint_y=None, height=256)
33
34    Example().run()

Reference❖

class HoverBehavior(**kwargs)❖

Bases: object

A mixin that adds hover detection to any widget.

Events

Todo

The current event implementation relies on __events__ being defined as a class attribute. This works for now, but it overrides inherited Kivy events.

When revisiting the event system, consider migrating to register_event_type() and updating the DocLang command to support dynamically registered events.

on_hover

Called when the widget’s hover state changes.

on_hover_enter

Called when the pointer begins hovering over the widget.

on_hover_leave

Called when the pointer stops hovering over the widget.

hovered: AliasProperty❖

Whether the widget is currently hovered.

hovered is a read-only AliasProperty that returns a bool.

Default value❖
False
on_hover(state: bool) → None❖

Called when the widget’s hover state changes.

Parameters

Name

Description

state

True when the widget is hovered and False otherwise.

This method is dispatched for both on_hover_enter and on_hover_leave transitions.

on_hover_enter() → None❖

Called when the pointer begins hovering over the widget.

on_hover_leave() → None❖

Called when the pointer stops hovering over the widget.