Hover

Module path
kivydk.uix.behavior.hover

Overview

This behavior adds simple hover awareness to widgets, allowing them to react when the mouse cursor moves over or away from them. It provides an easy way to create interactive, desktop‑friendly UI elements that respond visually or functionally to hover interactions.


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    """Each event updates the color and text to reflect the current hover state."""
10    def __init__(self, **kwargs):
11        super().__init__(**kwargs)
12
13        # Initialize the button with default information
14        self.on_hover()
15
16    def on_hover(self, *args):
17        """
18        Update the label text and color to display the current hover state.
19
20        :param args: Unused arguments from event callbacks.
21        """
22        self.background_color = [0.8, 0.4, 0.2, 1] if self.hovered else [1, 1, 1, 1]
23        self.text = "Hovered" if self.hovered else "Unhovered"
24
25
26#// RUN FILE
27if __name__ == "__main__":
28    from kivy.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 lightweight hover detection to any widget.

Events
on_hover

Called when the cursor enters and leaves the widget’s bounds.

on_hover_start

Called when the cursor enters the widget’s bounds.

on_hover_end

Called when the cursor leaves the widget’s bounds.

hovered: AliasProperty

Whether the mouse cursor is currently hovering over the widget.

hovered is a read-only AliasProperty that returns a bool and defaults to False.

on_hover(state: bool) None

Called when the cursor enters and leaves the widget’s bounds.

This method is dispatched every time the hover state changes, allowing widgets to react to both hover‑start and hover‑end transitions.

Parameters

Name

Description

state

True when the cursor is currently hovering over the widget and False otherwise.

on_hover_start() None

Called when the cursor enters the widget’s bounds.

on_hover_end() None

Called when the cursor leaves the widget’s bounds.