M5Switch
M5Switch is a widget that can be used to create switch in the user interface. It can be used to trigger actions when checked and uncheked.
UiFlow2 Example
event switch
Open the cores3_switch_event_example.m5f2 project in UiFlow2.
This example creates a switch that triggers an event when checked and uncheked.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
event switch
This example creates a switch that triggers an event when checked and uncheked.
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
M5Switch
- class m5ui.switch.M5Switch(*args, **kwargs)
Bases:
switchCreate a switch object.
- Parameters:
x (int) – The x position of the switch.
y (int) – The y position of the switch.
w (int) – The width of the switch.
h (int) – The height of the switch.
bg_c (int) – The color of the switch in the off state in hexadecimal format.
bg_c_checked (int) – The color of the switch in the on state in hexadecimal format.
circle_c (int) – This color refers to the color of the circle on the switch in hexadecimal format.
parent (lv.obj) – The parent object to attach the switch to. If not specified, the switch will be attached to the default screen.
UiFlow2 Code Block:
None
MicroPython Code Block:
from m5ui import M5Switch import lvgl as lv m5ui.init() switch_0 = M5Switch(x=120, y=80, w=60, h=30, bg_c=0xE7E3E7, color=0x2196F3, parent=page0)
- set_bg_color(color, opa, part)
Set the background color of the switch.
- Parameters:
UiFlow2 Code Block:


MicroPython Code Block:
switch_0.set_bg_color(0xE7E3E7, 255, lv.PART.MAIN | lv.STATE.DEFAULT) switch_0.set_bg_color(0x2196F3, 255, lv.PART.INDICATOR | lv.STATE.CHECKED)
- set_state(state, value)
Set the state of the Switch.
- Parameters:
UiFlow2 Code Block:

MicroPython Code Block:
switch_0.set_state(lv.STATE.CHECKED, True)
- has_state(state)
Get the state of the Switch.
- Parameters:
state (int) – The state to get.
- Returns:
True if the state is set, False otherwise.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
switch_0.has_state(lv.STATE.CHECKED)
- set_pos(x, y)
Set the position of the switch.
UiFlow2 Code Block:

MicroPython Code Block:
switch_0.set_pos(100, 100)
- set_x(x)
Set the x-coordinate of the switch.
- Parameters:
x (int) – The x-coordinate of the switch.
UiFlow2 Code Block:

MicroPython Code Block:
switch_0.set_x(100)
- set_y(y)
Set the y-coordinate of the switch.
- Parameters:
y (int) – The y-coordinate of the switch.
UiFlow2 Code Block:

MicroPython Code Block:
switch_0.set_y(100)
- set_size(width, height)
Set the size of the switch.
UiFlow2 Code Block:

MicroPython Code Block:
switch_0.set_size(100, 50)
- set_width(width)
Set the width of the switch.
- Parameters:
width (int) – The width of the switch.
UiFlow2 Code Block:

MicroPython Code Block:
switch_0.set_width(100)
- set_height(height)
Set the height of the switch.
- Parameters:
height (int) – The height of the switch.
UiFlow2 Code Block:

MicroPython Code Block:
switch_0.set_height(50)
- align_to(obj, align, x, y)
Align the switch to another object.
- Parameters:
UiFlow2 Code Block:

MicroPython Code Block:
switch_0.align_to(page_0, lv.ALIGN.CENTER, 0, 0)
- add_event_cb(handler, event, user_data)
Add an event callback to the switch. 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.
UiFlow2 Code Block:

MicroPython Code Block:
def switch0_checked_event(event_struct): global page0, button0 print("checked") def switch0_unchecked_event(event_struct): global page0, button0 print("unchecked") def switch0_event_handler(event_struct): global page0, button0 event = event_struct.code obj = event_struct.get_target_obj() if event == lv.EVENT.VALUE_CHANGED: if obj.has_state(lv.STATE.CHECKED): switch0_checked_event(event_struct) else: switch0_unchecked_event(event_struct) return switch_0.add_event_cb(switch0_event_handler, lv.EVENT.ALL, None)

