Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d73eea4

Browse files
Tinyu-Zhaolbuque
authored andcommitted
libs/m5ui: Add LVGL TabView widget.
Signed-off-by: tinyu <[email protected]>
1 parent 1914289 commit d73eea4

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

m5stack/libs/m5ui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"M5Spinbox": "spinbox",
3030
"M5Spinner": "spinner",
3131
"M5Switch": "switch",
32+
"M5TabView": "tabview",
3233
"M5TextArea": "textarea",
3334
"M5Win": "win",
3435
}

m5stack/libs/m5ui/manifest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"spinbox.py",
3232
"spinner.py",
3333
"switch.py",
34+
"tabview.py",
3435
"textarea.py",
3536
"win.py",
3637
),

m5stack/libs/m5ui/tabview.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
from m5ui.base import M5Base
6+
from m5ui.button import M5Button
7+
import lvgl as lv
8+
9+
10+
def _button_clicked_event_cb(event_struct):
11+
_button = event_struct.get_current_target()
12+
13+
if not hasattr(_button, "get_parent"):
14+
_button = lv.obj.__cast__(_button)
15+
16+
tv = _button.get_parent().get_parent()
17+
idx = _button.get_index()
18+
lv.tabview.set_active(tv, idx, False)
19+
20+
21+
class M5TabView(lv.tabview):
22+
"""Create a label object.
23+
24+
:param str text: The text to display on the label.
25+
:param int x: The x position of the label.
26+
:param int y: The y position of the label.
27+
:param int text_c: The text color of the label in hexadecimal format.
28+
:param int bg_c: The background color of the label in hexadecimal format.
29+
:param int bg_opa: The background opacity of the label (0-255).
30+
:param lv.lv_font_t font: The font to use for the button text.
31+
:param lv.obj parent: The parent object to attach the button to. If not specified, the button will be attached to the default screen.
32+
33+
UiFlow2 Code Block:
34+
35+
None
36+
37+
MicroPython Code Block:
38+
39+
.. code-block:: python
40+
41+
from m5ui import M5Label
42+
import lvgl as lv
43+
44+
m5ui.init()
45+
label_0 = M5Label(text="Hello, World!", x=10, y=10, text_c=0x212121, bg_c=0xFFFFFF, bg_opa=0, font=lv.font_montserrat_14, parent=page0)
46+
"""
47+
48+
def __init__(
49+
self,
50+
x=0,
51+
y=0,
52+
w=0,
53+
h=0,
54+
bar_size=60,
55+
bar_pos=lv.DIR.TOP,
56+
parent=None,
57+
):
58+
if parent is None:
59+
parent = lv.screen_active()
60+
super().__init__(parent)
61+
self.set_pos(x, y)
62+
self.set_size(w, h)
63+
self.set_tab_bar_position(bar_pos)
64+
self.set_tab_bar_size(bar_size)
65+
self.tab_num = 0
66+
67+
def add_tab(self, text):
68+
_button = M5Button(
69+
text=text,
70+
w=lv.pct(100),
71+
h=lv.pct(100),
72+
bg_c=0xFFFFFF,
73+
text_c=0x0,
74+
parent=self.get_tab_bar(),
75+
)
76+
_button.set_flex_grow(1)
77+
_button.add_event_cb(_button_clicked_event_cb, lv.EVENT.CLICKED, None)
78+
79+
_cont = self.get_content()
80+
_page = lv.obj(_cont)
81+
_page.set_size(lv.pct(100), lv.pct(100))
82+
self.tab_num += 1
83+
return _page
84+
85+
def rename_tab(self, pos: int, txt: str) -> None:
86+
if pos < self.tab_num:
87+
super().rename_tab(pos, txt)
88+
89+
def __getattr__(self, name):
90+
if hasattr(M5Base, name):
91+
method = getattr(M5Base, name)
92+
bound_method = lambda *args, **kwargs: method(self, *args, **kwargs)
93+
setattr(self, name, bound_method)
94+
return bound_method
95+
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")

0 commit comments

Comments
 (0)