class LabelPlus – 显示远程文本
LabelPlus 类扩展了 Widgets.Label 类,提供了处理动态更新文本的附加功能。
目前只接受json格式的字符串,并通过``json_key``提取数据。
UiFlow2 应用示例
简单用法
在 UiFlow2 中打开 cores3_labelplus_example.m5f2 项目。
此示例演示如何创建和操作 LabelPlus 控件。
UiFlow2 代码块:
示例输出:
None
MicroPython 应用示例
简单用法
此示例演示如何创建和操作 LabelPlus 控件。
MicroPython 代码块:
# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD # # SPDX-License-Identifier: MIT import os, sys, io import M5 from M5 import * from label_plus import LabelPlus label_plus0 = None en = None def btnPWR_wasClicked_event(state): # noqa: N802 global label_plus0, en en = not en if en: label_plus0.set_update_enable(True) else: label_plus0.set_update_enable(False) def setup(): global label_plus0, en M5.begin() Widgets.setRotation(1) Widgets.fillScreen(0x222222) label_plus0 = LabelPlus( "label_plus0", 24, 31, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18, "http://192.168.8.200:8000/data", 3000, True, "data", "error", 0xFF0000, ) BtnPWR.setCallback(type=BtnPWR.CB_TYPE.WAS_CLICKED, cb=btnPWR_wasClicked_event) en = True def loop(): global label_plus0, en M5.update() if __name__ == "__main__": try: setup() while True: loop() except (Exception, KeyboardInterrupt) as e: try: from utility import print_error_msg print_error_msg(e) except ImportError: print("please update to latest firmware")
示例输出:
None
API参考
LabelPlus
- class label_plus.LabelPlus(*args, **kwargs)
基类:
Label创建一个可以从 URL 获取和显示文本的 LabelPlus 对象。
- 参数:
text (str) – 标签上显示的初始文本。
x (int) – 标签的 x 坐标。
y (int) – 标签的 y 坐标。
size (int) – 标签文本的字体大小。
text_color (int) – 标签的文本颜色,十六进制格式。
bg_color (int) – 标签的背景颜色,十六进制格式。
font – 用于标签文本的字体。
url (str) – 从中获取数据的 URL。
period (int) – 更新周期(以毫秒为单位)。如果设置为 0,标签将不会自动更新。
enable (bool) – 是否启用自动更新。
json_key (str) – 要从获取的数据中提取的 JSON 键。
error_msg (str) – 出现错误时显示的消息。
error_msg_color (int) – 显示错误消息时使用的文本颜色。
UiFlow2 代码块:
None
MicroPython 代码块:
from label_plus import LabelPlus label_plus0 = LabelPlus("label_plus0", 7, 10, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18, "http://example.com", 3000, True, "title", "error", 0xFF0000)
- setText(text)
设置 LabelPlus 控件的文本。
- 参数:
text (str) – 要在标签上设置的文本。
UiFlow2 代码块:

MicroPython 代码块:
label_plus_0.setText("New Text")
- setCursor(x=0, y=0)
设置 LabelPlus 控件中文本光标的起始坐标。
UiFlow2 代码块:

MicroPython 代码块:
label_plus_0.setCursor(10, 20)
- setSize(size)
设置 LabelPlus 控件中文本的字体大小。
- 参数:
size (float) – 要设置的字体大小。
UiFlow2 代码块:

MicroPython 代码块:
label_plus_0.setSize(1.5)
- setFont(font)
设置 LabelPlus 控件中文本的字体。
- 参数:
font – 支持内置字体和字体文件(例如
.bin(lvgl 二进制字体格式)或.vlw(Processing 字体格式))。有关内置字体列表、状态以及设备支持的完整信息,请参见Display.setFont()。Widgets.FONTS 使用的字体与 M5.Display 相同。
UiFlow2 代码块:

MicroPython 代码块:
label_plus_0.setFont(Widgets.FONTS.Montserrat12)
- setVisible(visible)
设置 LabelPlus 控件的可见属性。
- 参数:
visible (bool) – True 使标签可见,False 隐藏它。
UiFlow2 代码块:

MicroPython 代码块:
label_plus_0.setVisible(True)
- set_update_enable(enable)
启用或禁用自动更新。
- 参数:
enable (bool) – True 启用自动更新,False 禁用。
UiFlow2 代码块:

MicroPython 代码块:
label_plus0.set_update_enable(True)
- set_update_period(period)
设置自动更新的更新周期。
- 参数:
period (int) – 更新周期(以毫秒为单位)。
UiFlow2 代码块:

MicroPython 代码块:
label_plus0.set_update_period(5000)
- is_valid_data()
检查当前数据是否有效(即不是错误消息)。
- 返回:
如果当前数据有效则返回 True,否则返回 False。
- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
valid = label_plus0.is_valid_data()
- get_data()
获取标签上显示的当前数据。
- 返回:
当前数据。
- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
data = label_plus0.get_data()

