From 5f6e07e57ff5d02c9010cc70c3d18f37b688aaa8 Mon Sep 17 00:00:00 2001 From: Amir Gonnen Date: Sun, 11 Jul 2021 18:39:08 +0300 Subject: [PATCH] feat(calendar) improve MicroPython example (#2366) Small improvements: - Remove cast from get_pressed_date - Check return value of get_pressed_date - Call set_today_date on clicked date - Compact highlighted_days - Added a switch to show different header type --- .../widgets/calendar/lv_example_calendar_1.py | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/examples/widgets/calendar/lv_example_calendar_1.py b/examples/widgets/calendar/lv_example_calendar_1.py index 112451ba2..04a27cda8 100644 --- a/examples/widgets/calendar/lv_example_calendar_1.py +++ b/examples/widgets/calendar/lv_example_calendar_1.py @@ -4,9 +4,9 @@ def event_handler(evt): if code == lv.EVENT.VALUE_CHANGED: source = evt.get_target() date = lv.calendar_date_t() - lv.calendar.get_pressed_date(source,date) - if date: - print("Clicked date: %02d.%02d.%02d"%(date.day, date.month, date.year)) + if source.get_pressed_date(date) == lv.RES.OK: + calendar.set_today_date(date.year, date.month, date.day) + print("Clicked date: %02d.%02d.%02d"%(date.day, date.month, date.year)) calendar = lv.calendar(lv.scr_act()) @@ -18,25 +18,32 @@ calendar.set_today_date(2021, 02, 23) calendar.set_showed_date(2021, 02) # Highlight a few days -highlighted_days=[] -for i in range(3): - highlighted_days.append(lv.calendar_date_t()) +highlighted_days=[ + lv.calendar_date_t({'year':2021, 'month':2, 'day':6}), + lv.calendar_date_t({'year':2021, 'month':2, 'day':11}), + lv.calendar_date_t({'year':2021, 'month':2, 'day':22}) +] -highlighted_days[0].year=2021 -highlighted_days[0].month=02 -highlighted_days[0].day=6 +calendar.set_highlighted_dates(highlighted_days, len(highlighted_days)) -highlighted_days[1].year=2021 -highlighted_days[1].month=02 -highlighted_days[1].day=11 +# 2 options for header +header1 = lv.calendar_header_dropdown(lv.scr_act(),calendar) +header2 = lv.calendar_header_arrow(lv.scr_act(),calendar,25) -highlighted_days[2].year=2022 -highlighted_days[2].month=02 -highlighted_days[2].day=22 +# Switch to switch headeres +header2.add_flag(lv.obj.FLAG.HIDDEN) +header1.clear_flag(lv.obj.FLAG.HIDDEN) -calendar.set_highlighted_dates(highlighted_days, 3) - -header = lv.calendar_header_dropdown(lv.scr_act(),calendar) -# header = lv.calendar_header_arrow(lv.scr_act(),calendar,25) +sw = lv.switch(lv.scr_act()) +sw.set_pos(20,20) +def sw_cb(e): + obj = e.get_target() + if obj.has_state(lv.STATE.CHECKED): + header1.add_flag(lv.obj.FLAG.HIDDEN) + header2.clear_flag(lv.obj.FLAG.HIDDEN) + else: + header2.add_flag(lv.obj.FLAG.HIDDEN) + header1.clear_flag(lv.obj.FLAG.HIDDEN) +sw.add_event_cb(sw_cb, lv.EVENT.VALUE_CHANGED, None)