1
-
2
1
# This example shows how to create a custom widget and a custom theme
3
2
4
3
##############################################################################
5
4
# Initializations
6
5
##############################################################################
7
6
8
7
import usys as sys
9
- sys .path .append ('' ) # See: https://github.com/micropython/micropython/issues/6419
8
+
9
+ sys .path .append ("" ) # See: https://github.com/micropython/micropython/issues/6419
10
10
11
11
import lvgl as lv
12
- import display_driver
13
12
14
13
lv .init ()
15
14
19
18
20
19
member_name_cache = {}
21
20
21
+
22
22
def get_member_name (obj , value ):
23
23
try :
24
24
return member_name_cache [id (obj )][id (value )]
@@ -33,13 +33,14 @@ def get_member_name(obj, value):
33
33
member_name_cache [id (obj )] = {id (value ): member }
34
34
return member
35
35
36
+
36
37
##############################################################################
37
38
# A class that describes a custom widget class
38
39
# An instance of this class can be used to create custom widgets
39
40
##############################################################################
40
41
41
- class CustomWidgetClass ():
42
42
43
+ class CustomWidgetClass :
43
44
def __init__ (self , width , height ):
44
45
# Define LVGL widget class
45
46
# In this example the class receive parameters (width, height)
@@ -87,10 +88,11 @@ def event_cb(self, lv_cls, e):
87
88
layer = e .get_layer ()
88
89
self .draw (obj , layer )
89
90
elif code in [
90
- lv .EVENT .STYLE_CHANGED ,
91
- lv .EVENT .VALUE_CHANGED ,
92
- lv .EVENT .PRESSING ,
93
- lv .EVENT .RELEASED ]:
91
+ lv .EVENT .STYLE_CHANGED ,
92
+ lv .EVENT .VALUE_CHANGED ,
93
+ lv .EVENT .PRESSING ,
94
+ lv .EVENT .RELEASED ,
95
+ ]:
94
96
# Check if need to recalculate widget parameters
95
97
obj .valid = False
96
98
@@ -101,15 +103,15 @@ def calc(self, obj):
101
103
102
104
obj .draw_desc = lv .draw_triangle_dsc_t ()
103
105
obj .draw_desc .init ()
104
- obj .draw_desc .bg_opa = lv .OPA .COVER
105
- obj .draw_desc .bg_color = obj .get_style_bg_color (lv .PART .MAIN )
106
+ obj .draw_desc .opa = lv .OPA .COVER
107
+ obj .draw_desc .color = obj .get_style_bg_color (lv .PART .MAIN )
106
108
obj .points = [
107
- {'x' : area . x1 + area . get_width () // 2 ,
108
- 'y' : area .y2 if obj . get_state () & lv . STATE . CHECKED else area . y1 } ,
109
- { 'x' : area .x2 ,
110
- 'y' : area . y1 + area . get_height () // 2 },
111
- {'x' : area .x1 ,
112
- 'y' : area .y1 + area .get_height ()// 2 },
109
+ {
110
+ "x" : area .x1 + area . get_width () // 2 ,
111
+ "y" : area .y2 if obj . get_state () & lv . STATE . CHECKED else area . y1 ,
112
+ },
113
+ {"x" : area .x2 , "y" : area . y1 + area . get_height () // 2 } ,
114
+ { "x" : area . x1 , "y" : area .y1 + area .get_height () // 2 },
113
115
]
114
116
obj .draw_desc .p [0 ] = obj .points [0 ]
115
117
obj .draw_desc .p [1 ] = obj .points [1 ]
@@ -125,15 +127,16 @@ def draw(self, obj, layer):
125
127
# Draw the custom widget
126
128
lv .draw_triangle (layer , obj .draw_desc )
127
129
130
+
128
131
##############################################################################
129
132
# A Python class to wrap the LVGL custom widget
130
133
##############################################################################
131
134
132
- class CustomWidget ():
133
135
136
+ class CustomWidget :
134
137
# An instance of a widget-class to be used for creating custom widgets
135
138
# d = lv.display_get_default()
136
- dpi = 130 # d.get_dpi()
139
+ dpi = 130 # d.get_dpi()
137
140
cls = CustomWidgetClass (dpi , dpi )
138
141
139
142
@staticmethod
@@ -142,15 +145,13 @@ def get_class():
142
145
return CustomWidget .cls .get_class ()
143
146
144
147
def __new__ (cls , parent ):
145
- # Return a new lv object instead of CustomWidget,
148
+ # Return a new lv object instead of CustomWidget,
146
149
# but first bind the LVGL object with CustomWidgetWrapper
147
150
wrapper = cls .CustomWidgetWrapper (parent )
148
151
return wrapper .lv_obj
149
152
150
- class CustomWidgetWrapper ():
151
-
153
+ class CustomWidgetWrapper :
152
154
def __init__ (self , parent ):
153
-
154
155
# Create the LVGL object from class
155
156
self .lv_obj = CustomWidget .cls .create (parent )
156
157
@@ -160,7 +161,6 @@ def __init__(self, parent):
160
161
# Initialize the object
161
162
self .lv_obj .class_init_obj ()
162
163
163
-
164
164
def __getattr__ (self , attr ):
165
165
# Provide access to LVGL object functions
166
166
# print("__getattr__(%s, %s)" % (repr(self), repr(attr)))
@@ -169,12 +169,13 @@ def __getattr__(self, attr):
169
169
def __repr__ (self ):
170
170
return "Custom Widget"
171
171
172
+
172
173
##############################################################################
173
174
# A theme to apply styles to the custom widget
174
175
##############################################################################
175
176
176
- class CustomTheme (lv .theme_t ):
177
177
178
+ class CustomTheme (lv .theme_t ):
178
179
class Style (lv .style_t ):
179
180
def __init__ (self ):
180
181
super ().__init__ ()
@@ -197,7 +198,6 @@ def __init__(self):
197
198
# Pressed color is blue
198
199
self .set_bg_color (lv .palette_main (lv .PALETTE .BLUE ))
199
200
200
-
201
201
def __init__ (self ):
202
202
super ().__init__ ()
203
203
self .custom_style = CustomTheme .Style ()
@@ -214,7 +214,7 @@ def __init__(self):
214
214
215
215
# Activate this theme on the default display
216
216
lv .display_get_default ().set_theme (self )
217
-
217
+
218
218
def apply (self , theme , obj ):
219
219
# Apply this theme on CustomWidget class
220
220
if obj .get_class () == CustomWidget .get_class ():
@@ -232,7 +232,9 @@ def apply(self, theme, obj):
232
232
# Create a screen with flex layout
233
233
scr = lv .screen_active ()
234
234
scr .set_flex_flow (lv .FLEX_FLOW .COLUMN )
235
- scr .set_flex_align (lv .FLEX_ALIGN .SPACE_EVENLY , lv .FLEX_ALIGN .CENTER , lv .FLEX_ALIGN .CENTER )
235
+ scr .set_flex_align (
236
+ lv .FLEX_ALIGN .SPACE_EVENLY , lv .FLEX_ALIGN .CENTER , lv .FLEX_ALIGN .CENTER
237
+ )
236
238
237
239
# Add a button with a label
238
240
button = lv .button (scr )
@@ -244,10 +246,11 @@ def apply(self, theme, obj):
244
246
l2 = lv .label (customWidget )
245
247
l2 .set_text ("Click me!" )
246
248
249
+
247
250
# Add click events to both button and custom widget
248
251
def event_cb (e ):
249
252
print ("%s Clicked!" % repr (e .get_target_obj ()))
250
253
254
+
251
255
for widget in [button , customWidget ]:
252
256
widget .add_event_cb (event_cb , lv .EVENT .CLICKED , None )
253
-
0 commit comments