M5Switch

M5Switch 是一个用于在用户界面中创建开关的控件,可在开关被选中或取消选中时触发操作。

UiFlow2 示例

开关事件

在 UiFlow2 中打开 cores3_switch_event_example.m5f2 项目。

本示例创建了一个开关,当其被选中或取消选中时会触发事件。

UiFlow2 代码块:

cores3_switch_event_example.png

示例输出:

None

MicroPython 示例

开关事件

本示例创建了一个开关,当其被选中或取消选中时会触发事件。

MicroPython 代码块:

 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")

示例输出:

None

API参考

M5Switch

class m5ui.switch.M5Switch(*args, **kwargs)

基类:switch

创建一个开关对象。

参数:
  • x (int) – 开关的 x 坐标。

  • y (int) – 开关的 y 坐标。

  • w (int) – 开关的宽度

  • h (int) – 开关的高度

  • bg_c (int) – 开关在关闭状态下的颜色(十六进制)。

  • bg_c_checked (int) – 开关在开启状态下的颜色(十六进制)。

  • circle_c (int) – 此颜色指开关圆钮的颜色(十六进制)。

  • parent (lv.obj) – 开关要附加到的父对象;若未指定,则附加到默认屏幕。

UiFlow2 代码块:

None

MicroPython 代码块:

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)

设置开关的背景颜色。

参数:
  • color (int) – 要设置的颜色。

  • opa (int) – 颜色的不透明度,取值范围 0(透明)至 255(不透明)。

  • part (int) – 应用样式的对象部分(如 lv.PART.MAIN)。

UiFlow2 代码块:

set_bg_color.png

set_knob_color.png

MicroPython 代码块:

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)

设置开关的状态。

参数:
  • state (int) – 要设置的状态。

  • value (bool) – 若为 True 表示设置状态;为 False 则取消状态。

UiFlow2 代码块:

set_state.png

MicroPython 代码块:

switch_0.set_state(lv.STATE.CHECKED, True)
has_state(state)

获取开关的状态。

参数:

state (int) – 要获取的状态。

返回:

若状态已设置则返回 True,否则返回 False。

返回类型:

bool

UiFlow2 代码块:

has_state.png

MicroPython 代码块:

switch_0.has_state(lv.STATE.CHECKED)
set_pos(x, y)

设置开关的位置。

参数:
  • x (int) – 开关的 x 坐标。

  • y (int) – 开关的 y 坐标。

UiFlow2 代码块:

set_pos.png

MicroPython 代码块:

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

设置开关的 x 坐标。

参数:

x (int) – 开关的 x 坐标。

UiFlow2 代码块:

set_x.png

MicroPython 代码块:

switch_0.set_x(100)
set_y(y)

设置开关的 y 坐标。

参数:

y (int) – 开关的 y 坐标。

UiFlow2 代码块:

set_y.png

MicroPython 代码块:

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

设置开关的尺寸。

参数:
  • width (int) – 开关的宽度

  • height (int) – 开关的高度

UiFlow2 代码块:

set_size.png

MicroPython 代码块:

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

设置开关的宽度。

参数:

width (int) – 开关的宽度

UiFlow2 代码块:

set_width.png

MicroPython 代码块:

switch_0.set_width(100)
set_height(height)

设置开关的高度。

参数:

height (int) – 开关的高度

UiFlow2 代码块:

set_height.png

MicroPython 代码块:

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

将开关对齐到另一个对象。

参数:
  • obj (lv.obj) – 要对齐到的对象。

  • align (int) – 对齐类型。

  • x (int) – 相对于对齐对象的 x 偏移量。

  • y (int) – 相对于对齐对象的 y 偏移量。

UiFlow2 代码块:

align_to.png

MicroPython 代码块:

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

为开关添加事件回调;当指定事件发生时将调用该回调。

参数:
  • handler (function) – 要调用的回调函数。

  • event (int) – 要监听的事件。

  • user_data (Any) – 可选的用户数据,将传递给回调函数。

UiFlow2 代码块:

event.png

MicroPython 代码块:

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)
set_direction(direction)

设置开关的方向。

参数:

direction (int) – 开关方向。 选项: - 0:水平 - 1:垂直

UiFlow2 代码块:

set_direction.png

MicroPython 代码块:

switch_0.set_direction(0)  # Set to horizontal
switch_0.set_direction(1)  # Set to vertical