Skip to content

Label

Usage

import toga

label = toga.Label("Hello world")

Notes

  • Winforms does not support a text alignment value of JUSTIFIED. If this alignment value is used, the label will default to left alignment.

Reference

Bases: Widget

Source code in core/src/toga/widgets/label.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Label(Widget):
    def __init__(
        self,
        text: str,
        id: str | None = None,
        style: StyleT | None = None,
        **kwargs,
    ):
        """Create a new text label.

        :param text: Text of the label.
        :param id: The ID for the widget.
        :param style: A style object. If no style is provided, a default style
            will be applied to the widget.
        :param kwargs: Initial style properties.
        """
        super().__init__(id, style, **kwargs)

        self.text = text

    def _create(self) -> Any:
        return self.factory.Label(interface=self)

    def focus(self) -> None:
        """No-op; Label cannot accept input focus."""
        pass

    @property
    def text(self) -> str:
        """The text displayed by the label.

        `None`, and the Unicode codepoint U+200B (ZERO WIDTH SPACE), will be
        interpreted and returned as an empty string. Any other object will be
        converted to a string using `str()`.
        """
        return self._impl.get_text()

    @text.setter
    def text(self, value: object) -> None:
        if value is None or value == "\u200b":
            text = ""
        else:
            text = str(value)

        self._impl.set_text(text)
        self.refresh()

text property writable

The text displayed by the label.

None, and the Unicode codepoint U+200B (ZERO WIDTH SPACE), will be interpreted and returned as an empty string. Any other object will be converted to a string using str().

__init__(text, id=None, style=None, **kwargs)

Create a new text label.

:param text: Text of the label. :param id: The ID for the widget. :param style: A style object. If no style is provided, a default style will be applied to the widget. :param kwargs: Initial style properties.

Source code in core/src/toga/widgets/label.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(
    self,
    text: str,
    id: str | None = None,
    style: StyleT | None = None,
    **kwargs,
):
    """Create a new text label.

    :param text: Text of the label.
    :param id: The ID for the widget.
    :param style: A style object. If no style is provided, a default style
        will be applied to the widget.
    :param kwargs: Initial style properties.
    """
    super().__init__(id, style, **kwargs)

    self.text = text

focus()

No-op; Label cannot accept input focus.

Source code in core/src/toga/widgets/label.py
31
32
33
def focus(self) -> None:
    """No-op; Label cannot accept input focus."""
    pass