M5Menu
M5Menu is a widget that can be used to create multi-level menus in the user interface.
UiFlow2 Example
menu event
Open the menu_core2_example.m5f2 project in UiFlow2.
This example creates a multi-level menus.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
menu event
This example creates a multi-level menus.
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 13menu0 = None 14Label1 = None 15Label2 = None 16Page_2 = None 17Page2_Label1 = None 18Page2_Label2 = None 19Page_3 = None 20Page3_Label = None 21Page3_Switch = None 22 23 24import random 25 26 27def Page3_Switch_checked_event(event_struct): # noqa: N802 28 global \ 29 page0, \ 30 menu0, \ 31 Label1, \ 32 Label2, \ 33 Page_2, \ 34 Page2_Label1, \ 35 Page2_Label2, \ 36 Page_3, \ 37 Page3_Label, \ 38 Page3_Switch 39 40 Page3_Label.set_text_color(random.randint(0x000000, 0xFFFFFF), 255, 0) 41 42 43def Page3_Switch_unchecked_event(event_struct): # noqa: N802 44 global \ 45 page0, \ 46 menu0, \ 47 Label1, \ 48 Label2, \ 49 Page_2, \ 50 Page2_Label1, \ 51 Page2_Label2, \ 52 Page_3, \ 53 Page3_Label, \ 54 Page3_Switch 55 56 Page3_Label.set_text_color(random.randint(0x000000, 0xFFFFFF), 255, 0) 57 58 59def Page3_Switch_event_handler(event_struct): # noqa: N802 60 global \ 61 page0, \ 62 menu0, \ 63 Label1, \ 64 Label2, \ 65 Page_2, \ 66 Page2_Label1, \ 67 Page2_Label2, \ 68 Page_3, \ 69 Page3_Label, \ 70 Page3_Switch 71 event = event_struct.code 72 obj = event_struct.get_target_obj() 73 if event == lv.EVENT.VALUE_CHANGED: 74 if obj.has_state(lv.STATE.CHECKED): 75 Page3_Switch_checked_event(event_struct) 76 else: 77 Page3_Switch_unchecked_event(event_struct) 78 return 79 80 81def setup(): 82 global \ 83 page0, \ 84 menu0, \ 85 Label1, \ 86 Label2, \ 87 Page_2, \ 88 Page2_Label1, \ 89 Page2_Label2, \ 90 Page_3, \ 91 Page3_Label, \ 92 Page3_Switch 93 94 M5.begin() 95 Widgets.setRotation(1) 96 m5ui.init() 97 page0 = m5ui.M5Page(bg_c=0xFFFFFF) 98 menu0 = m5ui.M5Menu(x=0, y=0, w=320, h=240, parent=page0) 99 menu0.set_page(menu0.main_page) 100 Label1 = menu0.add_label("Label1") 101 Label2 = menu0.add_label("Label2") 102 menu0.set_mode_root_back_button(lv.menu.ROOT_BACK_BUTTON.ENABLED) 103 menu0.set_mode_header(lv.menu.HEADER.TOP_FIXED) 104 105 page0.screen_load() 106 Label2.set_style_text_font(lv.font_montserrat_24, lv.PART.MAIN | lv.STATE.DEFAULT) 107 Label2.set_text(str("Click me!!!!!!!!")) 108 Page_2 = lv.menu_page(menu0, "Page2") 109 menu0.set_load_page_event(Label2.cont, Page_2) 110 Page2_Label1 = menu0.add_label("Label1", parent=Page_2) 111 Page2_Label2 = menu0.add_label( 112 "Label2 (Click me)", 113 text_c=0xFF0000, 114 text_opa=255, 115 bg_c=0xFFFFFF, 116 bg_opa=255, 117 font=lv.font_montserrat_24, 118 parent=Page_2, 119 ) 120 Page_3 = lv.menu_page(menu0, "Page3") 121 menu0.set_load_page_event(Page2_Label2.cont, Page_3) 122 Page3_Label = menu0.add_label("A Label", parent=Page_3) 123 Page3_Label.set_style_text_font(lv.font_montserrat_24, lv.PART.MAIN | lv.STATE.DEFAULT) 124 Page3_Switch = menu0.add_switch( 125 "A Switch", 126 w=80, 127 h=40, 128 bg_c=0xE7E3E7, 129 bg_opa=255, 130 bg_c_checked=0x2196F3, 131 bg_c_checked_opa=255, 132 circle_c=0xFF9966, 133 circle_opa=255, 134 parent=Page_3, 135 ) 136 Page3_Switch.add_event_cb(Page3_Switch_event_handler, lv.EVENT.ALL, None) 137 138 139def loop(): 140 global \ 141 page0, \ 142 menu0, \ 143 Label1, \ 144 Label2, \ 145 Page_2, \ 146 Page2_Label1, \ 147 Page2_Label2, \ 148 Page_3, \ 149 Page3_Label, \ 150 Page3_Switch 151 M5.update() 152 153 154if __name__ == "__main__": 155 try: 156 setup() 157 while True: 158 loop() 159 except (Exception, KeyboardInterrupt) as e: 160 try: 161 m5ui.deinit() 162 from utility import print_error_msg 163 164 print_error_msg(e) 165 except ImportError: 166 print("please update to latest firmware")
Example output:
None
API
M5Menu
- class m5ui.menu.M5Menu(*args, **kwargs)
Bases:
menuCreate a list object.
- Parameters:
x (int) – The x position of the menu.
y (int) – The y position of the menu.
w (int) – The width of the menu.
h (int) – The height of the menu.
page_name (str) – The name of the main page of the menu.
parent (lv.obj) – The parent object to attach the menu to. If not specified, the menu will be attached to the default screen.
UiFlow2 Code Block:
None
MicroPython Code Block:
from m5ui import M5Menu import lvgl as lv m5ui.init() menu0 = M5Menu(x=120, y=80, w=60, h=30, parent=page0)
- set_page(page)
Set main page for the menu.
- Parameters:
page (lv.obj) – The main page object.
UiFlow2 Code Block:

MicroPython Code Block:
menu0.set_page(menu0.main_page)
- set_mode_header(mode)
Set the mode header for the menu.
- Parameters:
mode (int) –
The mode header text.
Options:
lv.menu.HEADER.TOP_FIXEDlv.menu.HEADER.TOP_UNFIXEDlv.menu.HEADER.BOTTOM_FIXED
UiFlow2 Code Block:

MicroPython Code Block:
menu0.set_mode_header(lv.menu.HEADER.TOP_FIXED)
- set_pos(x, y)
Set the position of the menu.
UiFlow2 Code Block:

MicroPython Code Block:
menu0.set_pos(100, 100)
- set_size(width, height)
Set the size of the menu.
UiFlow2 Code Block:

MicroPython Code Block:
menu0.set_size(100, 50)
- add_label(text, text_c=2171169, text_opa=255, bg_c=16777215, bg_opa=255, font=lvgl.font_montserrat_14, parent=None)
Add a label to the menu.
- Parameters:
text (str) – The text to display on the label.
text_c (int) – The text color of the label.
bg_c (int) – The background color of the label.
bg_opa (int) – The background opacity of the label.
font (lv.font_t) – The font of the label.
parent (lv.obj) – The parent object to attach the label to. If not specified, the label will be attached to the main page of the menu.
- Returns:
The created label object.
- Return type:
-
UiFlow2 Code Block:


MicroPython Code Block:
label0 = menu0.add_label("Hello, World!", text_c=0x212121, bg_c=0xFFFFFF, bg_opa=255, font=lv.font_montserrat_14, parent=menu0.main_page)
- add_switch(text, w=50, h=20, bg_c=15197159, bg_opa=255, bg_c_checked=166139, bg_c_checked_opa=255, circle_c=16777215, circle_opa=255, parent=None)
Add a switch to the menu.
- Parameters:
text (str) – The text to display next to the switch.
w (int) – The width of the switch.
h (int) – The height of the switch.
bg_c (int) – The background color of the switch when unchecked.
bg_c_checked (int) – The background color of the switch when checked.
circle_c (int) – The color of the switch circle.
parent (lv.obj) – The parent object to attach the switch to. If not specified, the switch will be attached to the main page of the menu.
- Returns:
The created switch object.
- Return type:
UiFlow2 Code Block:


MicroPython Code Block:
switch_0 = menu0.add_switch("Switch 1", w=50, h=20, bg_c=0xE7E3E7, bg_c_checked=0x0288FB, circle_c=0xFFFFFF, parent=menu0.main_page)
