M5Bar

M5Bar is a widget that can be used to create progress bars in the user interface. It displays values within a specified range using a visual bar indicator. The bar can be customized with different colors, gradients, and can optionally display the current value as text.

UiFlow2 Example

Temperature meter

Open the cores3_temperature_meter_example.m5f2 project in UiFlow2.

This example demonstrates how to create a temperature meter that shows the current temperature.

UiFlow2 Code Block:

cores3_temperature_meter_example.png

Example output:

None

MicroPython Example

Temperature meter

This example demonstrates how to create a temperature meter that shows the current temperature.

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
10from hardware import I2C
11from hardware import Pin
12from unit import ENVPROUnit
13import time
14
15
16page0 = None
17bar0 = None
18label0 = None
19i2c0 = None
20envpro_0 = None
21
22
23def setup():
24    global page0, bar0, label0, i2c0, envpro_0
25
26    M5.begin()
27    Widgets.setRotation(1)
28    m5ui.init()
29    page0 = m5ui.M5Page(bg_c=0xFFFFFF)
30    bar0 = m5ui.M5Bar(
31        x=148,
32        y=21,
33        w=20,
34        h=200,
35        min_value=0,
36        max_value=50,
37        value=25,
38        bg_c=0x2193F3,
39        color=0x2193F3,
40        parent=page0,
41    )
42    label0 = m5ui.M5Label(
43        "label0",
44        x=181,
45        y=112,
46        text_c=0x000000,
47        bg_c=0xFFFFFF,
48        bg_opa=0,
49        font=lv.font_montserrat_14,
50        parent=page0,
51    )
52
53    i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
54    envpro_0 = ENVPROUnit(i2c0)
55    page0.screen_load()
56    bar0.set_bg_grad_color(
57        0xFF0000, 255, 0x0000FF, 255, lv.GRAD_DIR.VER, lv.PART.INDICATOR | lv.STATE.DEFAULT
58    )
59
60
61def loop():
62    global page0, bar0, label0, i2c0, envpro_0
63    M5.update()
64    bar0.set_value(int(envpro_0.get_temperature()), True)
65    label0.set_text(str(envpro_0.get_temperature()))
66    time.sleep(1)
67
68
69if __name__ == "__main__":
70    try:
71        setup()
72        while True:
73            loop()
74    except (Exception, KeyboardInterrupt) as e:
75        try:
76            m5ui.deinit()
77            from utility import print_error_msg
78
79            print_error_msg(e)
80        except ImportError:
81            print("please update to latest firmware")

Example output:

None

API

M5Bar

class m5ui.bar.M5Bar(*args, **kwargs)

Bases: bar

Initialize a new M5Bar widget.

Parameters:
  • x (int) – The x-coordinate of the bar.

  • y (int) – The y-coordinate of the bar.

  • w (int) – The width of the bar.

  • h (int) – The height of the bar.

  • min_value (int) – The minimum value of the bar range.

  • max_value (int) – The maximum value of the bar range.

  • value (int) – The initial value of the bar.

  • is_show_value (bool) – Whether to display the current value as text.

  • bg_c (int) – The background color of the bar.

  • color (int) – The indicator color of the bar.

  • parent (lv.obj) – The parent object. If None, uses the active screen.

Returns:

None

UiFlow2 Code Block:

None

MicroPython Code Block:

bar = M5Bar(x=50, y=50, w=200, h=30, min_value=0, max_value=100, value=50)
get_value()

Get the current value of the bar.

Returns:

The current value of the bar.

Return type:

int

UiFlow2 Code Block:

get_value.png

MicroPython Code Block:

current_value = bar.get_value()
get_min_value()

Get the minimum value of the bar range.

Returns:

The minimum value.

Return type:

int

UiFlow2 Code Block:

get_min_value.png

MicroPython Code Block:

min_val = bar.get_min_value()
get_max_value()

Get the maximum value of the bar range.

Returns:

The maximum value.

Return type:

int

UiFlow2 Code Block:

get_max_value.png

MicroPython Code Block:

max_val = bar.get_max_value()
set_flag(flag, value)

Set a flag on the object. If value is True, the flag is added; if False, the flag is removed.

Parameters:
  • flag (int) – The flag to set.

  • value (bool) – If True, the flag is added; if False, the flag is removed.

Returns:

None

UiFlow2 Code Block:

set_flag.png

MicroPython Code Block:

bar.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:

toggle_flag.png

MicroPython Code Block:

bar.toggle_flag(lv.obj.FLAG.HIDDEN)
set_state(state, value)

Set the state of the bar. If value is True, the state is set; if False, the state is unset.

Parameters:
  • state (int) – The state to set.

  • value (bool) – If True, the state is set; if False, the state is unset.

Returns:

None

UiFlow2 Code Block:

set_state.png

MicroPython Code Block:

bar.set_state(lv.STATE.PRESSED, True)
toggle_state(state)

Toggle the state of the bar. 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:

toggle_state.png

MicroPython Code Block:

bar.toggle_state(lv.STATE.PRESSED)
set_bg_color(color, opa, part)

Set the background color of the bar.

Parameters:
  • color (int) – The color to set.

  • opa (int) – The opacity of the color.

  • part (int) – The part of the object to apply the style to (e.g., lv.PART.MAIN).

Returns:

None

UiFlow2 Code Block:

set_bg_color.png

set_indicator_color.png

MicroPython Code Block:

bar.set_bg_color(lv.color_hex(0x000000), 255, lv.PART.MAIN | lv.STATE.DEFAULT)

bar.set_bg_color(lv.color_hex(0x000000), 255, lv.PART.INDICATOR | lv.STATE.DEFAULT)
set_bg_grad_color(color, opa, grad_color, grad_opd, grad_dir, part)

Set the background gradient color of the bar.

Parameters:
  • color (int) – The start color of the gradient, can be an integer (RGB).

  • opa (int) – The opacity of the start color (0-255).

  • grad_color (int) – The end color of the gradient, can be an integer (RGB).

  • grad_opd (int) – The opacity of the end color (0-255).

  • grad_dir (int) – The direction of the gradient (e.g., lv.GRAD_DIR.VER).

  • part (int) – The part of the object to apply the style to (e.g., lv.PART.MAIN).

Returns:

None

UiFlow2 Code Block:

set_bg_grad_color.png

set_indicator_grad_color.png

MicroPython Code Block:

bar.set_bg_grad_color(0x00FF00, 255, 0xFF0000, 255, lv.GRAD_DIR.HOR, lv.PART.MAIN | lv.STATE.DEFAULT)
bar.set_bg_grad_color(0x00FF00, 255, 0xFF0000, 255, lv.GRAD_DIR.HOR, lv.PART.INDICATOR | lv.STATE.DEFAULT)
set_pos(x, y)

Set the position of the bar.

Parameters:
  • x (int) – The x-coordinate of the bar.

  • y (int) – The y-coordinate of the bar.

Returns:

None

UiFlow2 Code Block:

set_pos.png

MicroPython Code Block:

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

Set the x-coordinate of the bar.

Parameters:

x (int) – The x-coordinate of the bar.

Returns:

None

UiFlow2 Code Block:

set_x.png

MicroPython Code Block:

bar.set_x(100)
set_y(y)

Set the y-coordinate of the bar.

Parameters:

y (int) – The y-coordinate of the bar.

Returns:

None

UiFlow2 Code Block:

set_y.png

MicroPython Code Block:

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

Set the size of the bar.

Parameters:
  • width (int) – The width of the bar.

  • height (int) – The height of the bar.

Returns:

None

UiFlow2 Code Block:

set_size.png

MicroPython Code Block:

bar.set_size(200, 30)
set_width(width)

Set the width of the bar.

Parameters:

width (int) – The width of the bar.

Returns:

None

UiFlow2 Code Block:

set_width.png

MicroPython Code Block:

bar.set_width(200)
get_width()

Get the width of the bar.

Returns:

The width of the bar.

Return type:

int

UiFlow2 Code Block:

get_width.png

MicroPython Code Block:

width = bar.get_width()
set_height(height)

Set the height of the bar.

Parameters:

height (int) – The height of the bar.

Returns:

None

UiFlow2 Code Block:

set_height.png

MicroPython Code Block:

bar.set_height(30)
get_height()

Get the height of the bar.

Returns:

The height of the bar.

Return type:

int

UiFlow2 Code Block:

get_height.png

MicroPython Code Block:

height = bar.get_height()
align_to(obj, align, x, y)

Align the bar to another object.

Parameters:
  • obj (lv.obj) – The object to align to.

  • align (int) – The alignment type.

  • x (int) – The x-offset from the aligned object.

  • y (int) – The y-offset from the aligned object.

Returns:

None

UiFlow2 Code Block:

align_to.png

MicroPython Code Block:

bar.align_to(page_0, lv.ALIGN.CENTER, 0, 0)
set_value(value, anim=False)

Set the current value of the bar.

Parameters:
  • value (int) – The value to set.

  • anim_enable (bool) – Whether to enable animation when changing the value.

  • anim (bool)

Returns:

None

Return type:

None

UiFlow2 Code Block:

set_value.png

MicroPython Code Block:

bar.set_value(75, True)
set_range(min_value, max_value)

Set the value range of the bar.

Parameters:
  • min_value (int) – The minimum value.

  • max_value (int) – The maximum value.

Returns:

None

Return type:

None

UiFlow2 Code Block:

set_range.png

MicroPython Code Block:

bar.set_range(0, 200)