adding micropython examples (#2286)
* adding micropython examples * adding micropython examples
This commit is contained in:
@@ -1,48 +1,28 @@
|
||||
def event_handler(obj, event):
|
||||
if event == lv.EVENT.VALUE_CHANGED:
|
||||
print("State: %s" % ("On" if obj.get_state() else "Off"))
|
||||
def event_handler(e):
|
||||
code = e.get_code()
|
||||
obj = lv.switch.__cast__(e.get_target())
|
||||
if code == lv.EVENT.VALUE_CHANGED:
|
||||
if obj.has_state(lv.STATE.CHECKED):
|
||||
print("State: on")
|
||||
else:
|
||||
print("State: off")
|
||||
|
||||
# Create styles for the switch
|
||||
bg_style = lv.style_t()
|
||||
indic_style = lv.style_t()
|
||||
knob_on_style = lv.style_t()
|
||||
knob_off_style = lv.style_t()
|
||||
|
||||
lv.style_copy(bg_style, lv.style_pretty)
|
||||
bg_style.body.radius = 800
|
||||
bg_style.body.padding.top = 6
|
||||
bg_style.body.padding.bottom = 6
|
||||
lv.scr_act().set_flex_flow(lv.FLEX_FLOW.COLUMN)
|
||||
lv.scr_act().set_flex_align(lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER)
|
||||
|
||||
lv.style_copy(indic_style, lv.style_pretty_color)
|
||||
indic_style.body.radius = 800
|
||||
indic_style.body.main_color = lv.color_hex(0x9fc8ef)
|
||||
indic_style.body.grad_color = lv.color_hex(0x9fc8ef)
|
||||
indic_style.body.padding.left = 0
|
||||
indic_style.body.padding.right = 0
|
||||
indic_style.body.padding.top = 0
|
||||
indic_style.body.padding.bottom = 0
|
||||
sw = lv.switch(lv.scr_act())
|
||||
sw.add_event_cb(event_handler,lv.EVENT.ALL, None)
|
||||
|
||||
lv.style_copy(knob_off_style, lv.style_pretty)
|
||||
knob_off_style.body.radius = 800
|
||||
knob_off_style.body.shadow.width = 4
|
||||
knob_off_style.body.shadow.type = lv.SHADOW.BOTTOM
|
||||
sw = lv.switch(lv.scr_act())
|
||||
sw.add_state(lv.STATE.CHECKED)
|
||||
sw.add_event_cb(event_handler, lv.EVENT.ALL, None)
|
||||
|
||||
lv.style_copy(knob_on_style, lv.style_pretty_color)
|
||||
knob_on_style.body.radius = 800
|
||||
knob_on_style.body.shadow.width = 4
|
||||
knob_on_style.body.shadow.type = lv.SHADOW.BOTTOM
|
||||
sw = lv.switch(lv.scr_act())
|
||||
sw.add_state(lv.STATE.DISABLED)
|
||||
sw.add_event_cb(event_handler, lv.EVENT.ALL, None)
|
||||
|
||||
# Create a switch and apply the styles
|
||||
sw1 = lv.sw(lv.scr_act())
|
||||
sw1.set_style(lv.sw.STYLE.BG, bg_style)
|
||||
sw1.set_style(lv.sw.STYLE.INDIC, indic_style)
|
||||
sw1.set_style(lv.sw.STYLE.KNOB_ON, knob_on_style)
|
||||
sw1.set_style(lv.sw.STYLE.KNOB_OFF, knob_off_style)
|
||||
sw1.align(None, lv.ALIGN.CENTER, 0, -50)
|
||||
sw1.set_event_cb(event_handler)
|
||||
sw = lv.switch(lv.scr_act())
|
||||
sw.add_state(STATE.CHECKED | lv.STATE.DISABLED)
|
||||
sw.add_event_cb(event_handler, lv.EVENT.ALL, None)
|
||||
|
||||
# Copy the first switch and turn it ON
|
||||
sw2 = lv.sw(lv.scr_act(), sw1)
|
||||
sw2.on(lv.ANIM.ON)
|
||||
sw2.align(None, lv.ALIGN.CENTER, 0, 50)
|
||||
sw2.set_event_cb(lambda o,e: None)
|
||||
Reference in New Issue
Block a user