Skip to content

Commit 01e186d

Browse files
Tinyu-Zhaolbuque
authored andcommitted
lib/m5ui: Add LVGL Win widget.
Signed-off-by: tinyu <[email protected]>
1 parent 12b9c62 commit 01e186d

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

m5stack/libs/m5ui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"M5Spinbox": "spinbox",
2626
"M5Switch": "switch",
2727
"M5TextArea": "textarea",
28+
"M5Win": "win",
2829
}
2930

3031

m5stack/libs/m5ui/manifest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"spinbox.py",
2929
"switch.py",
3030
"textarea.py",
31+
"win.py",
3132
),
3233
base_path="..",
3334
opt=0,

m5stack/libs/m5ui/win.py

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
from m5ui.base import M5Base
6+
import lvgl as lv
7+
import m5ui
8+
9+
10+
class M5Win(lv.win):
11+
"""Create a window object.
12+
13+
:param int x: The x position of the window.
14+
:param int y: The y position of the window.
15+
:param int w: The width of the window.
16+
:param int h: The height of the window.
17+
:param lv.obj parent: The parent object to attach the window to. If not specified, the window will be attached to the default screen.
18+
19+
UiFlow2 Code Block:
20+
21+
None
22+
23+
MicroPython Code Block:
24+
25+
.. code-block:: python
26+
27+
from m5ui import M5Win
28+
import lvgl as lv
29+
30+
m5ui.init()
31+
win0 = M5Win(x=120, y=80, w=60, h=30, parent=page0)
32+
"""
33+
34+
def __init__(
35+
self,
36+
x=0,
37+
y=0,
38+
w=0,
39+
h=0,
40+
parent=None,
41+
):
42+
if parent is None:
43+
parent = lv.screen_active()
44+
super().__init__(parent)
45+
self.set_pos(x, y)
46+
self.set_size(w, h)
47+
48+
def add_title(
49+
self,
50+
text,
51+
text_c=0x212121,
52+
text_opa=255,
53+
bg_c=0xE0E0E0,
54+
bg_opa=255,
55+
font=lv.font_montserrat_14,
56+
):
57+
"""Add a title label to the window.
58+
59+
:param str text: The text to display on the window.
60+
:param int text_c: The text color of the label in hexadecimal format.
61+
:param int text_opa: The text opacity of the label (0-255).
62+
:param int bg_c: The background color of the label in hexadecimal format.
63+
:param int bg_opa: The background opacity of the label (0-255).
64+
:param lv.font font: The font to use for the label.
65+
:return: The created label object :ref:`m5ui.M5Label <m5ui.M5Label>`.
66+
:rtype: lv.obj
67+
68+
UiFlow2 Code Block:
69+
70+
|add_title.png|
71+
72+
MicroPython Code Block:
73+
74+
.. code-block:: python
75+
76+
win0.add_title("A title", text_c=0x212121, text_opa=255, bg_c=0xE0E0E0, bg_opa=255, font=lv.font_montserrat_14)
77+
"""
78+
_label = m5ui.M5Label(
79+
text=text,
80+
text_c=text_c,
81+
bg_c=bg_c,
82+
bg_opa=bg_opa,
83+
font=font,
84+
parent=self.get_header(),
85+
)
86+
_label.set_long_mode(lv.label.LONG_MODE.DOTS)
87+
_label.set_flex_grow(1)
88+
_label.set_style_text_opa(text_opa, lv.PART.MAIN | lv.STATE.DEFAULT)
89+
return _label
90+
91+
def add_text(
92+
self,
93+
text,
94+
x=0,
95+
y=0,
96+
text_c=0x212121,
97+
text_opa=255,
98+
bg_c=0xF6F6F6,
99+
bg_opa=255,
100+
font=lv.font_montserrat_14,
101+
):
102+
"""Add a text label to the window.
103+
104+
:param str text: The text to display on the window.
105+
:param int x: The x position of the label.
106+
:param int y: The y position of the label.
107+
:param int text_c: The text color of the label in hexadecimal format.
108+
:param int text_opa: The text opacity of the label (0-255).
109+
:param int bg_c: The background color of the label in hexadecimal format.
110+
:param int bg_opa: The background opacity of the label (0-255).
111+
:param lv.font font: The font to use for the label.
112+
:return: The created label object :ref:`m5ui.M5Label <m5ui.M5Label>`.
113+
:rtype: lv.obj
114+
115+
UiFlow2 Code Block:
116+
117+
|add_text.png|
118+
119+
MicroPython Code Block:
120+
121+
.. code-block:: python
122+
123+
win0.add_text("A title", text_c=0x212121, text_opa=255, bg_c=0xF6F6F6, bg_opa=255, font=lv.font_montserrat_14)
124+
"""
125+
_label = m5ui.M5Label(
126+
text=text,
127+
x=x,
128+
y=y,
129+
text_c=text_c,
130+
bg_c=bg_c,
131+
bg_opa=bg_opa,
132+
font=font,
133+
parent=self.get_content(),
134+
)
135+
_label.set_style_text_opa(text_opa, lv.PART.MAIN | lv.STATE.DEFAULT)
136+
return _label
137+
138+
def add_button(
139+
self,
140+
icon=None,
141+
text="",
142+
w=lv.DPI_DEF // 3,
143+
bg_c=0x2196F3,
144+
bg_opa=255,
145+
text_c=0xFFFFFF,
146+
text_opa=255,
147+
font=lv.font_montserrat_14,
148+
):
149+
"""Add a button to the window.
150+
151+
:param int icon: The icon to display on the button.
152+
:param str text: The text to display on the button.
153+
:param int h: The height of the button.
154+
:param int bg_c: The background color of the button in hexadecimal format.
155+
:param int bg_opa: The background opacity of the button (0-255).
156+
:param int text_c: The text color of the button in hexadecimal format.
157+
:param int text_opa: The text opacity of the button (0-255).
158+
:param lv.font font: The font to use for the button text.
159+
:return: The created button object :ref:`m5ui.M5Button <m5ui.M5Button>`.
160+
:rtype: lv.obj
161+
162+
UiFlow2 Code Block:
163+
164+
|add_button.png|
165+
|add_button2.png|
166+
167+
MicroPython Code Block:
168+
169+
.. code-block:: python
170+
171+
win0.add_button(lv.SYMBOL.BULLET, text="", w=40, bg_c=0x2196F3)
172+
win0.add_button(text="btn", w=40, bg_c=0x2196F3, bg_opa=255, text_c=0xFFFFFF, text_opa=255, font=lv.font_montserrat_14)
173+
"""
174+
_h = lv.pct(100)
175+
_button = m5ui.M5Button(
176+
text=text,
177+
w=w,
178+
h=_h,
179+
bg_c=bg_c,
180+
text_c=text_c,
181+
font=font,
182+
parent=self.get_header(),
183+
)
184+
_button.set_style_bg_opa(bg_opa, lv.PART.MAIN | lv.STATE.DEFAULT)
185+
_button.set_style_text_opa(text_opa, lv.PART.MAIN | lv.STATE.DEFAULT)
186+
187+
if icon is not None:
188+
_icon = lv.image(_button)
189+
_icon.set_src(icon)
190+
_icon.set_align(lv.ALIGN.CENTER)
191+
return _button
192+
193+
def __getattr__(self, name):
194+
if hasattr(M5Base, name):
195+
method = getattr(M5Base, name)
196+
bound_method = lambda *args, **kwargs: method(self, *args, **kwargs)
197+
setattr(self, name, bound_method)
198+
return bound_method
199+
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")

0 commit comments

Comments
 (0)