Hover❖
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.
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:
objectA mixin that adds lightweight hover detection to any widget.
Events
on_hoverCalled when the cursor enters and leaves the widget’s bounds.
on_hover_startCalled when the cursor enters the widget’s bounds.
on_hover_endCalled when the cursor leaves the widget’s bounds.
- hovered: AliasProperty❖
Whether the mouse cursor is currently hovering over the widget.
hoveredis a read-onlyAliasPropertythat returns abooland 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
Truewhen the cursor is currently hovering over the widget andFalseotherwise.