M5Table

M5Table 由包含文本的行、列和单元格构成。

UiFlow2 应用示例

表格基本使用示例

在 UiFlow2 中打开 cores3_m5ui_table_example.m5f2 项目。

此示例演示了如何创建一个包含学生信息的表格,包括姓名、年龄和分数。该表格显示三名学生的数据:Alice (18, 95)、Bob (18, 80) 和 Carol (17, 86)。

UiFlow2 代码块:

cores3_m5ui_table_example.png

示例输出:

无。

MicroPython 示例

表格基本使用示例

此示例演示了如何创建一个包含学生信息的表格,包括姓名、年龄和分数。该表格显示三名学生的数据:Alice (18, 95)、Bob (18, 80) 和 Carol (17, 86)。

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
13table0 = None
14table = None
15i = None
16info = None
17row = None
18k = None
19
20
21def setup():
22    global page0, table0, table, i, info, row, k
23
24    M5.begin()
25    Widgets.setRotation(1)
26    m5ui.init()
27    page0 = m5ui.M5Page(bg_c=0xFFFFFF)
28    table0 = m5ui.M5Table(x=10, y=35, w=300, h=180, row_cnt=3, col_cnt=3, parent=page0)
29    table = m5ui.M5Label(
30        "M5UI Table Example",
31        x=35,
32        y=2,
33        text_c=0x0000FF,
34        bg_c=0x000000,
35        bg_opa=0,
36        font=lv.font_montserrat_24,
37        parent=page0,
38    )
39
40    page0.screen_load()
41    for i in range(3):
42        table0.set_column_width(i, 85)
43
44    table0.set_column_count(3)
45    table0.set_row_count(4)
46    table0.set_width(260)
47    table0.align_to(page0, lv.ALIGN.CENTER, 0, 10)
48    table0.set_cell_value(0, 0, "name")
49    table0.set_cell_value(0, 1, "age")
50    table0.set_cell_value(0, 2, "score")
51    info = {"name": ["Alice", "Bob", "Carol"], "age": [18, 18, 17], "score": [95, 80, 86]}
52    row = 1
53    for k in info["name"]:
54        table0.set_cell_value(row, 0, k)
55        row = row + 1
56    row = 1
57    for k in info["age"]:
58        table0.set_cell_value(row, 1, str(k))
59        row = row + 1
60    row = 1
61    for k in info["score"]:
62        table0.set_cell_value(row, 2, str(k))
63        row = row + 1
64
65
66def loop():
67    global page0, table0, table, i, info, row, k
68    M5.update()
69
70
71if __name__ == "__main__":
72    try:
73        setup()
74        while True:
75            loop()
76    except (Exception, KeyboardInterrupt) as e:
77        try:
78            m5ui.deinit()
79            from utility import print_error_msg
80
81            print_error_msg(e)
82        except ImportError:
83            print("please update to latest firmware")

示例输出:

无。

API 应用

M5Table

class m5ui.table.M5Table(*args, **kwargs)

基类:table

创建表格对象。

参数:
  • x (int) – 表格的 x 坐标位置。

  • y (int) – 表格的 y 坐标位置。

  • w (int) – 表格的宽度。

  • h (int) – 表格的高度。

  • row_cnt (int) – 行数。

  • col_cnt (int) – 列数。

  • parent (lv.obj) – 要附加表格的父对象。如果未指定,表格将附加到默认屏幕。

UiFlow2 代码块:

None

MicroPython 代码块:

from m5ui import M5Table
import lvgl as lv

m5ui.init()
table_0 = M5Table(x=30, y=20, w=200, h=150, row_cnt=2, col_cnt=2)
set_cell_value(row, col, value)

Set the value of a cell.

New rows/columns are added automatically if required.

参数:
  • row (int) – Row index [0 .. row_cnt - 1]

  • col (int) – Column index [0 .. col_cnt - 1]

  • value (str) – Text to display in the cell

返回:

None

UiFlow2 代码块:

set_cell_value.png

MicroPython 代码块:

table_0.set_cell_value(row, col, value)
get_cell_value(row, col)

Get the value of a cell.

参数:
  • row (int) – Row index

  • col (int) – Column index

返回:

Text in the cell

返回类型:

str

UiFlow2 代码块:

get_cell_value.png

MicroPython 代码块:

table_0.get_cell_value()
set_row_count(row_cnt)

Set the number of rows.

参数:

row_cnt (int) – 行数。

返回:

None

UiFlow2 代码块:

set_row_count.png

MicroPython 代码块:

table_0.set_row_count(row_cnt)
set_column_count(col_cnt)

Set the number of columns.

参数:

col_cnt (int) – 列数。

返回:

None

UiFlow2 代码块:

set_column_count.png

MicroPython 代码块:

table_0.set_column_count(col_cnt)
get_row_count()

Get the number of rows.

返回:

Number of row.

返回类型:

int

UiFlow2 代码块:

get_row_count.png

MicroPython 代码块:

row_cnt = table_0.get_row_count()
get_column_count()

Get the number of columns.

返回:

列数。

返回类型:

int

UiFlow2 代码块:

get_column_count.png

MicroPython 代码块:

col_cnt = table_0.get_column_count()
set_column_width(col, width)

Set the width of a column.

参数:
  • col (int) – Column index [0 .. LV_TABLE_COL_MAX - 1].

  • width (int) – Column width.

返回:

None

UiFlow2 代码块:

set_column_width.png

MicroPython 代码块:

table_0.set_column_width(col, width)
get_column_width(col)

Get the width of a column.

参数:

col (int) – Column index [0 .. LV_TABLE_COL_MAX - 1].

返回:

Column width.

返回类型:

int

UiFlow2 代码块:

get_column_width.png

MicroPython 代码块:

width = table_0.get_column_width()
set_pos(x, y)

Set the position of the Table.

param int x:

The x position of the Table.

param int y:

The y position of the Table.

return:

None

UiFlow2 代码块:

set_pos.png

MicroPython 代码块:

table_0.set_pos(x, y)
set_x(x)

Set the x position of the Table.

参数:

x (int) – The x position of the Table.

返回:

None

UiFlow2 代码块:

set_x.png

MicroPython 代码块:

table_0.set_x(x)
set_y(y)

Set the y position of the Table.

参数:

y (int) – The y position of the Table.

返回:

None

UiFlow2 代码块:

set_y.png

MicroPython 代码块:

table_0.set_y(y)
get_x()

Get the x position of the Table.

返回:

The x position of the Table.

返回类型:

int

UiFlow2 代码块:

get_x.png

MicroPython 代码块:

x = table_0.get_x()
get_y()

Get the y position of the Table.

返回:

The y position of the Table.

返回类型:

int

UiFlow2 代码块:

get_y.png

MicroPython 代码块:

y = table_0.get_y()
set_size(width, height)

Set the size of the Table.

参数:
  • width (int) – The width of the Table.

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

返回:

None

UiFlow2 代码块:

set_size.png

MicroPython 代码块:

table_0.set_size(width, height)
set_width(width)

Set the width of the Table.

参数:

width (int) – The width of the Table.

返回:

None

UiFlow2 代码块:

set_width.png

MicroPython 代码块:

table_0.set_width(width)
get_width()

Get the width of the Table.

返回:

The width of the Table.

返回类型:

int

UiFlow2 代码块:

get_width.png

MicroPython 代码块:

width = table_0.get_width()
set_height(height)

Set the height of the Table.

参数:

height (int) – The height of the Table.

返回:

None

UiFlow2 代码块:

set_height.png

MicroPython 代码块:

table_0.set_height(height)
get_height()

Get the height of the Table.

返回:

The height of the Table.

返回类型:

int

UiFlow2 代码块:

get_height.png

MicroPython 代码块:

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

Align the Table relative to another object.

参数:
  • obj – The reference object (e.g. page0).

  • align (int) – Alignment option (see lv.ALIGN constants below).

  • x (int) – X offset after alignment.

  • y (int) – Y offset after alignment.

返回:

None

UiFlow2 代码块:

align_to.png

MicroPython 代码块:

table_0.align_to(page0, lv.ALIGN.CENTER, 0, 0)
lv.ALIGN

Alignment options for positioning objects.

  • lv.ALIGN.DEFAULT

  • lv.ALIGN.TOP_LEFT

  • lv.ALIGN.TOP_MID

  • lv.ALIGN.TOP_RIGHT

  • lv.ALIGN.BOTTOM_LEFT

  • lv.ALIGN.BOTTOM_MID

  • lv.ALIGN.BOTTOM_RIGHT

  • lv.ALIGN.LEFT_MID

  • lv.ALIGN.RIGHT_MID

  • lv.ALIGN.CENTER

  • lv.ALIGN.OUT_TOP_LEFT

  • lv.ALIGN.OUT_TOP_MID

  • lv.ALIGN.OUT_TOP_RIGHT

  • lv.ALIGN.OUT_BOTTOM_LEFT

  • lv.ALIGN.OUT_BOTTOM_MID

  • lv.ALIGN.OUT_BOTTOM_RIGHT

  • lv.ALIGN.OUT_LEFT_TOP

  • lv.ALIGN.OUT_LEFT_MID

  • lv.ALIGN.OUT_LEFT_BOTTOM

  • lv.ALIGN.OUT_RIGHT_TOP

  • lv.ALIGN.OUT_RIGHT_MID

  • lv.ALIGN.OUT_RIGHT_BOTTOM