M5Msgbox

M5Msgbox is a widget that can be used to create msgboxes in the user interface.

UiFlow2 Example

msgbox event

Open the msgbox_core2_example.m5f2 project in UiFlow2.

This example creates msgbox and associated with events.

UiFlow2 Code Block:

msgbox_core2_example.png

Example output:

None

MicroPython Example

msgbox event

This example creates msgbox and associated with events.

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
13msgbox0 = None
14label_sfu = None
15btn_apply = None
16btn_cancel = None
17
18
19import random
20
21
22def btn_apply_clicked_event(event_struct):
23    global page0, msgbox0, label_sfu, btn_apply, btn_cancel
24
25    label_sfu.set_text(str((str("Hello ") + str((random.randint(1, 100))))))
26
27
28def btn_cancel_clicked_event(event_struct):
29    global page0, msgbox0, label_sfu, btn_apply, btn_cancel
30
31    btn_apply.toggle_flag(lv.obj.FLAG.HIDDEN)
32
33
34def btn_apply_event_handler(event_struct):
35    global page0, msgbox0, label_sfu, btn_apply, btn_cancel
36    event = event_struct.code
37    if event == lv.EVENT.CLICKED and True:
38        btn_apply_clicked_event(event_struct)
39    return
40
41
42def btn_cancel_event_handler(event_struct):
43    global page0, msgbox0, label_sfu, btn_apply, btn_cancel
44    event = event_struct.code
45    if event == lv.EVENT.CLICKED and True:
46        btn_cancel_clicked_event(event_struct)
47    return
48
49
50def setup():
51    global page0, msgbox0, label_sfu, btn_apply, btn_cancel
52
53    M5.begin()
54    Widgets.setRotation(1)
55    m5ui.init()
56    page0 = m5ui.M5Page(bg_c=0xFFFFFF)
57    msgbox0 = m5ui.M5Msgbox(title="Message Box", x=0, y=0, w=320, h=240, parent=page0)
58    label_sfu = msgbox0.add_text("This is label_sfu")
59    btn_apply = msgbox0.add_button(text="Apply", option="footer")
60    btn_cancel = msgbox0.add_button(text="Cancel", option="footer")
61    msgbox0.add_close_button()
62
63    btn_apply.add_event_cb(btn_apply_event_handler, lv.EVENT.ALL, None)
64    btn_cancel.add_event_cb(btn_cancel_event_handler, lv.EVENT.ALL, None)
65
66    page0.screen_load()
67
68
69def loop():
70    global page0, msgbox0, label_sfu, btn_apply, btn_cancel
71    M5.update()
72
73
74if __name__ == "__main__":
75    try:
76        setup()
77        while True:
78            loop()
79    except (Exception, KeyboardInterrupt) as e:
80        try:
81            m5ui.deinit()
82            from utility import print_error_msg
83
84            print_error_msg(e)
85        except ImportError:
86            print("please update to latest firmware")

Example output:

None

API

M5Msgbox

class m5ui.msgbox.M5Msgbox(*args, **kwargs)

Bases: msgbox

Create a msgbox object.

Parameters:
  • title (str) – The title of the msgbox.

  • x (int) – The x-coordinate of the msgbox.

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

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

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

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

MicroPython Code Block:

from m5ui import M5Msgbox
import lvgl as lv

m5ui.init()
msgbox_0 = M5Msgbox(
    title="msgbox",
    x=0,
    y=0,
    w=320,
    h=240,
    parent=page0,
)
add_close_button()

Add a close button to the msgboxheader.

Returns:

None

UiFlow2 Code Block:

add_close_button.png

MicroPython Code Block:

msgbox_0.add_close_button()
delete()

Delete the item from the msgbox.

UiFlow2 Code Block:

label_delete.png

button_delete.png

MicroPython Code Block:

button_0.delete()
text_0.delete()
set_text(txt)

Set text of the msgbox button/label.

Parameters:

txt (str) – The text to set for the msgbox button/label.

Returns:

None

UiFlow2 Code Block:

button_set_text.png

label_set_text.png

MicroPython Code Block:

button_0.set_text("Select an option")

label_0.set_text("M5Stack")
set_style_text_font(font, part)

Set the font of the msgbox button/label text.

Parameters:
  • font (lv.lv_font_t) – The font to set.

  • part (int) – The part of the object to apply the style to (e.g., lv.PART.MAIN).

Returns:

None

UiFlow2 Code Block:

button_set_font.png

label_set_font.png

MicroPython Code Block:

label_0.set_style_text_font(lv.font_montserrat_14, lv.PART.MAIN | lv.STATE.DEFAULT)

button_0.set_style_text_font(lv.font_montserrat_14, lv.PART.MAIN | lv.STATE.DEFAULT)
set_text_color(color, opa, part)

Set the color of the msgbox button/label.

Parameters:
  • color (int) – The color to set.

  • opa (int) – The opacity of the color.

  • part (int) – The part of the object to apply the style to (e.g., lv.PART.MAIN).

Returns:

None

UiFlow2 Code Block:

button_set_text_color.png

label_set_text_color.png

MicroPython Code Block:

button_0.set_text_color(lv.color_hex(0x000000), 255, lv.PART.MAIN | lv.STATE.DEFAULT)

label_0.set_text_color(lv.color_hex(0x000000), 255, lv.PART.MAIN | lv.STATE.DEFAULT)
set_bg_color(color, opa, part)

Set the background color of the msgbox label.

Parameters:
  • color (int) – The color to set.

  • opa (int) – The opacity of the color.

  • part (int) – The part of the object to apply the style to (e.g., lv.PART.MAIN).

Returns:

None

UiFlow2 Code Block:

button_set_bg_color.png

label_set_bg_color.png

MicroPython Code Block:

button_0.set_bg_color(lv.color_hex(0x000000), 255, lv.PART.MAIN | lv.STATE.DEFAULT)

label_0.set_bg_color(lv.color_hex(0x000000), 255, lv.PART.MAIN | lv.STATE.DEFAULT)
set_long_mode(mode)

Set the long mode of the msgbox label.

Parameters:

mode (int) – The long mode to set.

UiFlow2 Code Block:

label_set_long_mode.png

MicroPython Code Block:

label_0.set_long_mode(lv.label.LONG_MODE.WRAP)
set_flag(flag, value)

Set a flag on the object. If value is True, the flag is added; if False, the flag is removed.

Parameters:
  • flag (int) – The flag to set.

  • value (bool) – If True, the flag is added; if False, the flag is removed.

UiFlow2 Code Block:

set_flag.png

label_set_flag.png

button_set_flag.png

MicroPython Code Block:

msgbox_0.set_flag(lv.obj.FLAG.HIDDEN, True)
set_pos(x, y)

Set the position of the msgbox.

Parameters:
  • x (int) – The x-coordinate of the msgbox.

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

UiFlow2 Code Block:

set_pos.png

MicroPython Code Block:

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

Set the x-coordinate of the msgbox.

Parameters:

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

UiFlow2 Code Block:

set_x.png

MicroPython Code Block:

msgbox_0.set_x(100)
set_y(y)

Set the y-coordinate of the msgbox.

Parameters:

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

UiFlow2 Code Block:

set_y.png

MicroPython Code Block:

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

Set the size of the msgbox.

Parameters:
  • width (int) – The width of the msgbox.

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

Returns:

None

UiFlow2 Code Block:

set_size.png

MicroPython Code Block:

msgbox_0.set_size(100, 50)
align_to(obj, align, x, y)

Align the msgboxto another object.

Parameters:
  • 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:

msgbox_0.align_to(page_0, lv.ALIGN.CENTER, 0, 0)
set_state(state, value)

Set the state of the bar. If value is True, the state is set; if False, the state is unset.

Parameters:
  • state (int) – The state to set.

  • value (bool) – If True, the state is set; if False, the state is unset.

Returns:

None

UiFlow2 Code Block:

button_set_state.png

MicroPython Code Block:

msgbox_0.set_state(lv.STATE.PRESSED, True)
toggle_flag(flag)

Toggle a flag on the object. If the flag is set, it is removed; if not set, it is added.

Parameters:

flag (int) – The flag to toggle.

Returns:

None

UiFlow2 Code Block:

button_toggle_flag.png

MicroPython Code Block:

msgbox_0.toggle_flag(lv.obj.FLAG.HIDDEN)
set_style_radius(radius, part)

Set the corner radius of the msgbox button.

Parameters:
  • radius (int) – The radius to set.

  • part (int) – The part of the object to apply the style to (e.g., lv.PART.MAIN).

Returns:

None

UiFlow2 Code Block:

button_set_radius.png

MicroPython Code Block:

button_0.set_style_radius(10, lv.PART.MAIN | lv.STATE.DEFAULT)
set_shadow(color, opa, align, offset_x, offset_y)

Set a shadow for the label.

Parameters:
  • color (int) – The color of the shadow in hexadecimal format or an integer.

  • opa (int) – The opacity of the shadow (0-255).

  • align (int) – The alignment of the shadow relative to the label.

  • offset_x (int) – The horizontal offset of the shadow.

  • offset_y (int) – The vertical offset of the shadow.

Returns:

None

UiFlow2 Code Block:

label_set_shadow.png

MicroPython Code Block:

label_0.set_shadow(color=0x000000, opa=128, align=lv.ALIGN.BOTTOM_RIGHT, offset_x=5, offset_y=5)
unset_shadow()

Remove the shadow from the label.

UiFlow2 Code Block:

label_unset_shadow.png

MicroPython Code Block:

label_0.unset_shadow()
get_text()

Get the text of the label.

Returns:

The text of the label.

Return type:

str

UiFlow2 Code Block:

button_get_text.png

label_get_text.png

MicroPython Code Block:

label_0.get_text()
button_0.get_text()
toggle_state(state)

Toggle the state of the button. If the state is set, it is unset; if not set, it is set.

Parameters:

state (int) – The state to toggle.

Returns:

None

UiFlow2 Code Block:

button_toggle_state.png

MicroPython Code Block:

button_0.toggle_state(lv.STATE.PRESSED)
add_event_cb(handler, event, user_data)

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

Parameters:
  • 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.

Returns:

None

UiFlow2 Code Block:

button_event.png

MicroPython Code Block:

def btn_ono_clicked_event(event_struct):
    global page0, msgbox_0, label_lkg, btn_ono, btn_pjm, label0

    print('hello M5')


def btn_ono_event_handler(event_struct):
    global page0, msgbox_0, label_lkg, btn_ono, btn_pjm, label0
    event = event_struct.code
    if event == lv.EVENT.CLICKED and True:
        btn_ono_clicked_event(event_struct)
    return

btn_ono.add_event_cb(btn_ono_event_handler, lv.EVENT.ALL, None)
add_text(text, text_c=2171169, text_opa=255, bg_c=16777215, bg_opa=255, font=lvgl.font_montserrat_14)

Add a text label to the msgbox.

Parameters:
  • text (str) – The text to display.

  • text_c (int) – The text color in hexadecimal format.

  • text_opa (int) – The text opacity (0-255).

  • bg_c (int) – The background color in hexadecimal format.

  • bg_opa (int) – The background opacity (0-255).

  • font (lv.font) – The font to use for the text.

Returns:

The created label object.

Return type:

m5ui.M5Label

UiFlow2 Code Block:

add_text.png

add_text2.png

MicroPython Code Block:

text0 = msgbox_0.add_text(
    text="Hello World",
    text_c=0x212121,
    text_opa=255,
    bg_c=0xFFFFFF,
    bg_opa=255,
    font=lv.font_montserrat_14,
)
add_button(icon=None, text='', bg_c=2201331, bg_opa=255, text_c=16777215, text_opa=255, font=lvgl.font_montserrat_14, option='footer')

Add a button to the msgbox.

Parameters:
  • icon (str) – The icon to display on the button.

  • text (str) – The text to display on the button.

  • bg_c (int) – The background color in hexadecimal format.

  • bg_opa (int) – The background opacity (0-255).

  • text_c (int) – The text color in hexadecimal format.

  • text_opa (int) – The text opacity (0-255).

  • font (lv.font) – The font to use for the button text.

  • option (str) – The position of the button (“header” or “footer”).

Returns:

The created button object.

Return type:

m5ui.M5Button

UiFlow2 Code Block:

add_button.png

add_button2.png

MicroPython Code Block:

button0 = msgbox_0.add_button(
    icon=None,
    text="OK",
    bg_c=0x2196F3,
    bg_opa=255,
    text_c=0xFFFFFF,
    text_opa=255,
    font=lv.font_montserrat_14,
    option="footer",
)