1-
21# This example shows how to create a custom widget and a custom theme
32
43##############################################################################
54# Initializations
65##############################################################################
76
87import 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
1010
1111import lvgl as lv
12- import display_driver
1312
1413lv .init ()
1514
1918
2019member_name_cache = {}
2120
21+
2222def get_member_name (obj , value ):
2323 try :
2424 return member_name_cache [id (obj )][id (value )]
@@ -33,13 +33,14 @@ def get_member_name(obj, value):
3333 member_name_cache [id (obj )] = {id (value ): member }
3434 return member
3535
36+
3637##############################################################################
3738# A class that describes a custom widget class
3839# An instance of this class can be used to create custom widgets
3940##############################################################################
4041
41- class CustomWidgetClass ():
4242
43+ class CustomWidgetClass :
4344 def __init__ (self , width , height ):
4445 # Define LVGL widget class
4546 # In this example the class receive parameters (width, height)
@@ -87,10 +88,11 @@ def event_cb(self, lv_cls, e):
8788 layer = e .get_layer ()
8889 self .draw (obj , layer )
8990 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+ ]:
9496 # Check if need to recalculate widget parameters
9597 obj .valid = False
9698
@@ -101,15 +103,15 @@ def calc(self, obj):
101103
102104 obj .draw_desc = lv .draw_triangle_dsc_t ()
103105 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 )
106108 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 },
113115 ]
114116 obj .draw_desc .p [0 ] = obj .points [0 ]
115117 obj .draw_desc .p [1 ] = obj .points [1 ]
@@ -125,15 +127,16 @@ def draw(self, obj, layer):
125127 # Draw the custom widget
126128 lv .draw_triangle (layer , obj .draw_desc )
127129
130+
128131##############################################################################
129132# A Python class to wrap the LVGL custom widget
130133##############################################################################
131134
132- class CustomWidget ():
133135
136+ class CustomWidget :
134137 # An instance of a widget-class to be used for creating custom widgets
135138 # d = lv.display_get_default()
136- dpi = 130 # d.get_dpi()
139+ dpi = 130 # d.get_dpi()
137140 cls = CustomWidgetClass (dpi , dpi )
138141
139142 @staticmethod
@@ -142,15 +145,13 @@ def get_class():
142145 return CustomWidget .cls .get_class ()
143146
144147 def __new__ (cls , parent ):
145- # Return a new lv object instead of CustomWidget,
148+ # Return a new lv object instead of CustomWidget,
146149 # but first bind the LVGL object with CustomWidgetWrapper
147150 wrapper = cls .CustomWidgetWrapper (parent )
148151 return wrapper .lv_obj
149152
150- class CustomWidgetWrapper ():
151-
153+ class CustomWidgetWrapper :
152154 def __init__ (self , parent ):
153-
154155 # Create the LVGL object from class
155156 self .lv_obj = CustomWidget .cls .create (parent )
156157
@@ -160,7 +161,6 @@ def __init__(self, parent):
160161 # Initialize the object
161162 self .lv_obj .class_init_obj ()
162163
163-
164164 def __getattr__ (self , attr ):
165165 # Provide access to LVGL object functions
166166 # print("__getattr__(%s, %s)" % (repr(self), repr(attr)))
@@ -169,12 +169,13 @@ def __getattr__(self, attr):
169169 def __repr__ (self ):
170170 return "Custom Widget"
171171
172+
172173##############################################################################
173174# A theme to apply styles to the custom widget
174175##############################################################################
175176
176- class CustomTheme (lv .theme_t ):
177177
178+ class CustomTheme (lv .theme_t ):
178179 class Style (lv .style_t ):
179180 def __init__ (self ):
180181 super ().__init__ ()
@@ -197,7 +198,6 @@ def __init__(self):
197198 # Pressed color is blue
198199 self .set_bg_color (lv .palette_main (lv .PALETTE .BLUE ))
199200
200-
201201 def __init__ (self ):
202202 super ().__init__ ()
203203 self .custom_style = CustomTheme .Style ()
@@ -214,7 +214,7 @@ def __init__(self):
214214
215215 # Activate this theme on the default display
216216 lv .display_get_default ().set_theme (self )
217-
217+
218218 def apply (self , theme , obj ):
219219 # Apply this theme on CustomWidget class
220220 if obj .get_class () == CustomWidget .get_class ():
@@ -232,7 +232,9 @@ def apply(self, theme, obj):
232232# Create a screen with flex layout
233233scr = lv .screen_active ()
234234scr .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+ )
236238
237239# Add a button with a label
238240button = lv .button (scr )
@@ -244,10 +246,11 @@ def apply(self, theme, obj):
244246l2 = lv .label (customWidget )
245247l2 .set_text ("Click me!" )
246248
249+
247250# Add click events to both button and custom widget
248251def event_cb (e ):
249252 print ("%s Clicked!" % repr (e .get_target_obj ()))
250253
254+
251255for widget in [button , customWidget ]:
252256 widget .add_event_cb (event_cb , lv .EVENT .CLICKED , None )
253-
0 commit comments