class Label – display text

Label is the basic object type used to display text.

Micropython Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8
 9
10label0 = None
11
12
13def setup():
14    global label0
15
16    M5.begin()
17    Widgets.fillScreen(0x222222)
18    label0 = Widgets.Label("Text", 38, 47, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
19
20    label0.setText(str("Label"))
21    label0.setFont(Widgets.FONTS.DejaVu12)
22
23
24def loop():
25    global label0
26    M5.update()
27
28
29if __name__ == "__main__":
30    try:
31        setup()
32        while True:
33            loop()
34    except (Exception, KeyboardInterrupt) as e:
35        try:
36            from utility import print_error_msg
37
38            print_error_msg(e)
39        except ImportError:
40            print("please update to latest firmware")

UIFLOW2 Example:

example.png

cores3_label_example.m5f2

Constructors

class Widgets.Label(text: str, x: int, y: int, text_sz: float, text_c: int = 0xFFFFFF, bg_c: int = 0x000000, font=None, parent=None)

Create a Label object. It accepts the following parameters:

  • text is the text to be displayed.

  • x is the starting X-axis coordinate displayed.

  • y is the starting Y-axis coordinate displayed.

  • text_sz is the font size for displaying text, usually 1.0.

  • text_c is the font color for displaying text. The default is white.

  • bg_c is the background color of the displayed text, the default is black.

  • font is the set of fonts used to display text. For the built-in fonts, see Widgets.FONTS.

  • parent is the output target of the Label object, the default output is to the LCD, can also be output to the Canvas.

Methods

Label.setColor(text_c: int, bg_c: int = -1)

Sets the text font color of the Label object. Accept the following parameters:

  • text_c is the font color for displaying text.

  • bg_c is the background color of the displayed text, the default is black.

UIFLOW2:

setColor.png

Label.setCursor(x: int, y: int)

Sets the starting coordinates of the Label object. Accept the following parameters:

  • x is the starting X-axis coordinate displayed.

  • y is the starting Y-axis coordinate displayed.

UIFLOW2:

setCursor.png

Label.setFont(font)

Sets the font set of the Label object. font is the set of fonts used to display text, the built-in font, see Widgets.FONTS.

UIFLOW2:

setFont.png

Label.setSize(text_sz: float)

Set the text font size of the Label object, text_sz is the font size of the displayed text.

UIFLOW2:

setSize.png

Label.setText(text: str)

Sets the text content of the Label object.

UIFLOW2:

setText.png

Label.setVisible(visible: bool)

Set the visible property of the Label object, when visible is True, the Label object content will be visible, otherwise it will not be visible.

UIFLOW2:

setVisible.png