M5Calendar

M5Calendar is a widget that can be used to create a calendar in the user interface. It can be used to display and select dates.

UiFlow2 Example

event calendar

Open the cores3_calendar_event_example.m5f2 project in UiFlow2.

This example creates a calendar that triggers an event when the date is changed.

UiFlow2 Code Block:

cores3_calendar_event_example.png

Example output:

None

MicroPython Example

event calendar

This example creates a calendar that triggers an event when the date is changed.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8import m5ui
 9import lvgl as lv
10
11
12page0 = None
13switch0 = None
14
15
16def switch0_checked_event(event_struct):
17    global page0, switch0
18
19    print("switch0 checked")
20
21
22def switch0_unchecked_event(event_struct):
23    global page0, switch0
24
25    print("switch0 unchecked")
26
27
28def switch0_event_handler(event_struct):
29    global page0, switch0
30    event = event_struct.code
31    obj = event_struct.get_target_obj()
32    if event == lv.EVENT.VALUE_CHANGED:
33        if obj.has_state(lv.STATE.CHECKED):
34            switch0_checked_event(event_struct)
35        else:
36            switch0_unchecked_event(event_struct)
37    return
38
39
40def setup():
41    global page0, switch0
42
43    M5.begin()
44    Widgets.setRotation(1)
45    m5ui.init()
46    page0 = m5ui.M5Page(bg_c=0xFFFFFF)
47    switch0 = m5ui.M5Switch(
48        x=128,
49        y=91,
50        w=60,
51        h=30,
52        bg_c=0xE7E3E7,
53        bg_c_checked=0x2196F3,
54        circle_c=0xFFFFFF,
55        parent=page0,
56    )
57
58    switch0.add_event_cb(switch0_event_handler, lv.EVENT.ALL, None)
59
60    page0.screen_load()
61    switch0.set_bg_color(0x666666, 255, lv.PART.MAIN | lv.STATE.DEFAULT)
62    switch0.set_bg_color(0x33FF33, 255, lv.PART.INDICATOR | lv.STATE.CHECKED)
63
64
65def loop():
66    global page0, switch0
67    M5.update()
68
69
70if __name__ == "__main__":
71    try:
72        setup()
73        while True:
74            loop()
75    except (Exception, KeyboardInterrupt) as e:
76        try:
77            m5ui.deinit()
78            from utility import print_error_msg
79
80            print_error_msg(e)
81        except ImportError:
82            print("please update to latest firmware")

Example output:

None

API

M5Calendar

class m5ui.calendar.M5Calendar(*args, **kwargs)

基类:calendar

Create a calendar object.

参数:
  • x (int) – The x position of the calendar.

  • y (int) – The y position of the calendar.

  • w (int) – The width of the calendar.

  • h (int) – The height of the calendar.

  • style (str) – The style of the calendar, can be “arrow” or “dropdown” and None.

  • today_date (list) – The date to highlight as today in the format [year, month, day].

  • show_month (list) – The month to show in the format [year, month].

  • parent (lv.obj) – The parent object to attach the calendar to. If not specified, the calendar will be attached to the default screen.

UiFlow2 Code Block:

None

MicroPython Code Block:

from m5ui import M5Calendar
import lvgl as lv

m5ui.init()
calendar_0 = M5Calendar(x=0, y=0, w=200, h=200, style=None, today_date=[2024, 1, 1], show_month=[2024, 1], parent=page0)
set_month_shown(year, month)

Set the month and year shown in the calendar.

参数:
  • year (int) – The year to show.

  • month (int) – The month to show.

UiFlow2 Code Block:

set_month_shown.png

MicroPython Code Block:

calendar_0.set_month_shown(2023, 3)
set_pos(x, y)

Set the position of the calendar.

参数:
  • x (int) – The x-coordinate of the calendar.

  • y (int) – The y-coordinate of the calendar.

UiFlow2 Code Block:

set_pos.png

MicroPython Code Block:

calendar_0.set_pos(100, 100)
set_x(x)

Set the x-coordinate of the calendar.

参数:

x (int) – The x-coordinate of the calendar.

UiFlow2 Code Block:

set_x.png

MicroPython Code Block:

calendar_0.set_x(100)
set_y(y)

Set the y-coordinate of the calendar.

参数:

y (int) – The y-coordinate of the calendar.

UiFlow2 Code Block:

set_y.png

MicroPython Code Block:

calendar_0.set_y(100)
set_size(width, height)

Set the size of the calendar.

参数:
  • width (int) – The width of the calendar.

  • height (int) – The height of the calendar.

UiFlow2 Code Block:

set_size.png

MicroPython Code Block:

calendar_0.set_size(100, 50)
set_width(width)

Set the width of the calendar.

参数:

width (int) – The width of the calendar.

UiFlow2 Code Block:

set_width.png

MicroPython Code Block:

calendar_0.set_width(100)
set_height(height)

Set the height of the calendar.

参数:

height (int) – The height of the calendar.

UiFlow2 Code Block:

set_height.png

MicroPython Code Block:

calendar_0.set_height(50)
align_to(obj, align, x, y)

Align the calendar to another object.

参数:
  • obj (lv.obj) – The object to align to.

  • align (int) – The alignment type.

  • x (int) – The x-offset from the aligned object.

  • y (int) – The y-offset from the aligned object.

UiFlow2 Code Block:

align_to.png

MicroPython Code Block:

calendar_0.align_to(page_0, lv.ALIGN.CENTER, 0, 0)
add_event_cb(handler, event, user_data)

Add an event callback to the calendar. The callback will be called when the specified event occurs.

参数:
  • handler (function) – The callback function to call.

  • event (int) – The event to listen for.

  • user_data (Any) – Optional user data to pass to the callback.

UiFlow2 Code Block:

event.png

MicroPython Code Block:

def calendar_event_handler(event_struct):
    if event_struct.get_code() == lv.EVENT.VALUE_CHANGED:
        date = lv.calendar_date_t()
        if calendar_0.get_pressed_date(date) == lv.RESULT.OK:
            calendar_0.set_today_date(date.year, date.month, date.day)
            print("Clicked date: %02d.%02d.%02d" % (date.year, date.month, date.day))

calendar_0.add_event_cb(calendar_event_handler, lv.EVENT.ALL, None)
set_calendar_style(style)

Set the style of the calendar header.

参数:

style (str) – The style of the calendar header, can be “arrow”, “dropdown”, or None.

UiFlow2 Code Block:

set_calendar_style.png

MicroPython Code Block:

calendar_0.set_calendar_style("arrow")
calendar_0.set_calendar_style("dropdown")
calendar_0.set_calendar_style(None)
set_highlighted_dates(dates)

Set the highlighted dates in the calendar.

参数:

dates (list) – A list of dates to highlight in the format [year, month, day, year, month, day, …]

UiFlow2 Code Block:

set_highlighted_dates.png

MicroPython Code Block:

calendar_0.set_highlighted_dates([2024, 1, 1, 2024, 1, 2, 2024, 1, 3])