Click❖
kivydk.uix.behavior.click
Overview❖
This behavior encapsulates mouse click detection for widgets. It listens for press and release events, tracks the click state and triggers callbacks when a valid click occurs. This behavior removes the need for manual input handling and provides a consistent, reusable pattern for widgets that respond to user clicks.
Tip
Click detection is based on touch events, so finger presses on touch‑enabled monitors are treated as clicks.
Example❖
Sample demonstrating how ClickBehavior can be used to detect and handle click events.
1#// IMPORT
2from kivy.uix.label import Label
3
4from kivydk.uix.behavior import ClickBehavior
5
6
7#// LOGIC
8class TestClick(ClickBehavior, Label):
9 """Each event updates the label text to reflect the current click state."""
10 def __init__(self, **kwargs):
11 super().__init__(**kwargs)
12
13 # Local variables to count the clicks
14 self.count_click = 0
15 self.count_double_click = 0
16
17 # Initialize the label with default information
18 self.update_text()
19
20 # Register event to update the text for press/release
21 # `state` is classic kivy property
22 self.bind(state=self.update_text)
23
24 def on_click(self):
25 self.count_click += 1
26 self.update_text()
27
28 def on_double_click(self):
29 self.count_double_click += 1
30 self.update_text()
31
32 def update_text(self, *args):
33 """
34 Update the label text to display the current click state.
35
36 :type args: tuple[Any, ...]
37 :param args: Unused arguments from event callbacks.
38 """
39 state = "Click state: %s" % ("press" if self.state == "down" else "release")
40 single_click = f"Click's amount: %.2d" % self.count_click
41 double_click = f"Double click's amount: %.2d" % self.count_double_click
42
43 self.text = f"{single_click}\n{double_click}\n\n{state}"
44
45
46#// RUN FILE
47if __name__ == "__main__":
48 from kivy.app import App
49
50 class Example(App):
51 def build(self):
52 return TestClick(size_hint_y=None, height=256)
53
54 Example().run()
Reference❖
- class ClickBehavior(**kwargs)❖
Bases:
ButtonBehaviorA mixin that adds lightweight click and double click detection to any widget.
This extends
ButtonBehavior, so the standardon_pressandon_releaseevents remain available.Events
on_clickCalled when the widget is pressed and released.
on_double_clickCalled when the widget is pressed and released two times.
- click_interval: NumericProperty❖
Maximum time
(seconds)in which a second click is considered part of a double‑click.The sweet spot is between
0.2and0.3.click_intervalis aNumericPropertyand defaults to 0.25.