|
| 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