4
4
5
5
from .base import M5Base
6
6
import lvgl as lv
7
+ import warnings
7
8
8
9
9
10
class M5Slider (lv .slider ):
@@ -21,6 +22,19 @@ class M5Slider(lv.slider):
21
22
:param bg_c: The background color of the slider.
22
23
:param color: The color of the slider indicator.
23
24
:param parent: The parent object of the slider. If not specified, it will be set to the active screen.
25
+
26
+ UiFlow2 Code Block:
27
+
28
+ None
29
+
30
+ MicroPython Code Block:
31
+
32
+ .. code-block:: python
33
+
34
+ from m5ui import M5Slider
35
+ import lvgl as lv
36
+
37
+ slider_0 = M5Slider(x=50, y=50, w=200, h=20, min_value=0, max_value=100, value=25)
24
38
"""
25
39
26
40
def __init__ (
@@ -44,11 +58,60 @@ def __init__(
44
58
self .set_size (w , h )
45
59
self .set_pos (x , y )
46
60
self .set_mode (mode )
47
- self .set_range (min_value , max_value )
48
- self .set_value (value , False )
61
+ super () .set_range (min_value , max_value )
62
+ super () .set_value (value , False )
49
63
self .set_bg_color (bg_c , 51 , lv .PART .MAIN | lv .STATE .DEFAULT )
50
64
self .set_bg_color (color , lv .OPA .COVER , lv .PART .INDICATOR | lv .STATE .DEFAULT )
51
65
66
+ def set_value (self , value : int , anim : bool = False ) -> None :
67
+ """Set the value of the slider.
68
+
69
+ :param int value: The value to set.
70
+ :param bool anim: Whether to animate the change.
71
+ :return: None
72
+
73
+ UiFlow2 Code Block:
74
+
75
+ |set_value.png|
76
+
77
+ MicroPython Code Block:
78
+
79
+ .. code-block:: python
80
+
81
+ slider_0.set_value(50, True)
82
+ """
83
+ if not isinstance (value , int ):
84
+ raise ValueError ("Value must be an integer." )
85
+ if value < self .get_min_value ():
86
+ warnings .warn (f"Value is less than min_value, setting to { self .get_min_value ()} ." )
87
+ value = self .get_min_value ()
88
+ if value > self .get_max_value ():
89
+ warnings .warn (f"Value is greater than max_value, setting to { self .get_max_value ()} ." )
90
+ value = self .get_max_value ()
91
+ super ().set_value (value , anim )
92
+
93
+ def set_range (self , min_value : int , max_value : int ) -> None :
94
+ """Set the range of the slider.
95
+
96
+ :param int min_value: The minimum value of the range.
97
+ :param int max_value: The maximum value of the range.
98
+ :return: None
99
+
100
+ UiFlow2 Code Block:
101
+
102
+ |set_range.png|
103
+
104
+ MicroPython Code Block:
105
+
106
+ .. code-block:: python
107
+
108
+ slider_0.set_range(0, 200)
109
+ """
110
+ if not isinstance (min_value , int ) or not isinstance (max_value , int ):
111
+ raise ValueError ("min_value and max_value must be integers." )
112
+ super ().set_range (min_value , max_value )
113
+ self .set_value (self .get_value (), False )
114
+
52
115
def set_style_radius (self , radius : int , part : int ) -> None :
53
116
if radius < 0 :
54
117
raise ValueError ("Radius must be a non-negative integer." )
0 commit comments