M5Button
M5Button is a widget that can be used to create buttons in the user interface. It can be used to trigger actions when clicked.
UiFlow2 Example
event button
Open the cores3_button_event_example.m5f2 project in UiFlow2.
This example creates a button that triggers an event when clicked.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
event button
This example creates a button that triggers an event when clicked.
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 13button0 = None 14label0 = None 15 16 17def button0_pressed_event(event_struct): 18 global page0, button0, label0 19 20 label0.set_text(str("pressed")) 21 22 23def button0_released_event(event_struct): 24 global page0, button0, label0 25 26 label0.set_text(str("released")) 27 28 29def button0_event_handler(event_struct): 30 global page0, button0, label0 31 event = event_struct.code 32 if event == lv.EVENT.PRESSED and True: 33 button0_pressed_event(event_struct) 34 if event == lv.EVENT.RELEASED and True: 35 button0_released_event(event_struct) 36 return 37 38 39def setup(): 40 global page0, button0, label0 41 42 M5.begin() 43 m5ui.init() 44 page0 = m5ui.M5Screen(bg_c=0xFFFFFF) 45 button0 = m5ui.M5Button( 46 text="click me", 47 x=117, 48 y=102, 49 bg_c=0x2196F3, 50 text_c=0xFFFFFF, 51 font=lv.font_montserrat_14, 52 parent=page0, 53 ) 54 label0 = m5ui.M5Label( 55 "label0", 56 x=136, 57 y=33, 58 text_c=0x000000, 59 bg_c=0xFFFFFF, 60 bg_opa=0, 61 font=lv.font_montserrat_14, 62 parent=page0, 63 ) 64 65 button0.add_event_cb(button0_event_handler, lv.EVENT.ALL, None) 66 67 page0.screen_load() 68 69 70def loop(): 71 global page0, button0, label0 72 M5.update() 73 74 75if __name__ == "__main__": 76 try: 77 setup() 78 while True: 79 loop() 80 except (Exception, KeyboardInterrupt) as e: 81 try: 82 m5ui.deinit() 83 from utility import print_error_msg 84 85 print_error_msg(e) 86 except ImportError: 87 print("please update to latest firmware")
Example output:
None
API
M5Button
- class m5ui.button.M5Button(*args, **kwargs)
Bases:
buttonCreate a button object.
- Parameters:
text (str) – The text to display on the button.
x (int) – The x position of the button.
y (int) – The y position of the button.
w (int) – The width of the button. when set to 0, the button will automatically size to fit the text.
h (int) – The height of the button. when set to 0, the button will automatically size to fit the text.
bg_c (int) – The background color of the button in hexadecimal format.
text_c (int) – The text color of the button in hexadecimal format.
font (lv.lv_font_t) – The font to use for the button text.
parent (lv.obj) – The parent object to attach the button to. If not specified, the button will be attached to the default screen.
UiFlow2 Code Block:
None
MicroPython Code Block:
from m5ui import M5Button import lvgl as lv m5ui.init() button_0 = M5Button(text="Click Me", x=10, y=10, bg_c=0x2196F3, text_c=0xFFFFFF, parent=page0)
- set_flag(flag, value)
Set a flag on the object. If
valueis True, the flag is added; if False, the flag is removed.- Parameters:
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.set_flag(lv.obj.FLAG.HIDDEN, 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:

MicroPython Code Block:
button_0.toggle_flag(lv.obj.FLAG.HIDDEN)
- set_state(state, value)
Set the state of the button. If
valueis True, the state is set; if False, the state is unset.- Parameters:
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.set_state(lv.STATE.PRESSED, True)
- 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:

MicroPython Code Block:
button_0.toggle_state(lv.STATE.PRESSED)
- set_style_text_font(font, part)
Set the font of the button 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:

MicroPython Code Block:
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 button text.
- Parameters:
- Returns:
None
UiFlow2 Code Block:


MicroPython Code Block:
button_0.set_text_color(lv.color_hex(0x000000), 255, lv.PART.MAIN | lv.STATE.DEFAULT) button_0.set_text_color(lv.color_hex(0x000000), 255, lv.PART.MAIN | lv.STATE.PRESSED)
- set_bg_color(color, opa, part)
Set the background color of the button.
- Parameters:
- Returns:
None
UiFlow2 Code Block:


MicroPython Code Block:
button_0.set_bg_color(lv.color_hex(0x000000), 255, lv.PART.MAIN | lv.STATE.DEFAULT) button_0.set_bg_color(lv.color_hex(0x000000), 255, lv.PART.MAIN | lv.STATE.PRESSED)
- set_pos(x, y)
Set the position of the button.
- Parameters:
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.set_pos(100, 100)
- set_x(x)
Set the x-coordinate of the button.
- Parameters:
x (int) – The x-coordinate of the button.
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.set_x(100)
- set_y(y)
Set the y-coordinate of the button.
- Parameters:
y (int) – The y-coordinate of the button.
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.set_y(100)
- set_size(width, height)
Set the size of the button.
- Parameters:
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.set_size(100, 50)
- set_width(width)
Set the width of the button.
- Parameters:
width (int) – The width of the button.
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.set_width(100)
- set_height(height)
Set the height of the button.
- Parameters:
height (int) – The height of the button.
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.set_height(50)
- align_to(obj, align, x, y)
Align the button to another object.
- Parameters:
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.align_to(page_0, lv.ALIGN.CENTER, 0, 0)
- set_style_radius(radius, part)
Set the corner radius of the button.
- Parameters:
- Returns:
None
UiFlow2 Code Block:

MicroPython Code Block:
button_0.set_style_radius(10, lv.PART.MAIN | lv.STATE.DEFAULT)
- 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:

MicroPython Code Block:
def button_0_pressed_event(event_struct): global button_0 button_0.set_bg_color(0x000000, 255, 0) def button_0_released_event(event_struct): global button_0 button_0.set_bg_color(0xffffff, 255, 0) def button_0_clicked_event(event_struct): global button_0 button_0.set_bg_color(0x000000, 255, 0) def button_0_event_handler(event_struct): global button_0 event = event_struct.code if event == lv.EVENT.PRESSED and True: button_0_pressed_event(event_struct) if event == lv.EVENT.RELEASED and True: button_0_released_event(event_struct) if event == lv.EVENT.CLICKED and True: button_0_clicked_event(event_struct) if event == lv.EVENT.LONG_PRESSED and True: button_0_long_pressed_event(event_struct) return page_0.add_event_cb(button_0_event_handler, lv.EVENT.ALL, None)


