:right-sidebar: True Label =================================================================== .. currentmodule:: gi.repository.Gtk .. class:: Label(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.Gtk.Widget`, :class:`~gi.repository.GObject.InitiallyUnowned`, :class:`~gi.repository.GObject.Object` Implemented Interfaces: :class:`~gi.repository.Gtk.Accessible`, :class:`~gi.repository.Gtk.AccessibleText`, :class:`~gi.repository.Gtk.Buildable`, :class:`~gi.repository.Gtk.ConstraintTarget` :Constructors: :: Label(**properties) new(str:str=None) -> Gtk.Widget new_with_mnemonic(str:str=None) -> Gtk.Widget Constructors ------------ .. rst-class:: interim-class .. class:: Label :no-index: .. classmethod:: new(str: str | None = None) -> ~gi.repository.Gtk.Widget Creates a new label with the given text inside it. You can pass :const:`None` to get an empty label widget. :param str: The text of the label .. classmethod:: new_with_mnemonic(str: str | None = None) -> ~gi.repository.Gtk.Widget Creates a new ``GtkLabel``, containing the text in ``str``. If characters in ``str`` are preceded by an underscore, they are underlined. If you need a literal underscore character in a label, use '``__``' (two underscores). The first underlined character represents a keyboard accelerator called a mnemonic. The mnemonic key can be used to activate another widget, chosen automatically, or explicitly using :obj:`~gi.repository.Gtk.Label.set_mnemonic_widget`. If :obj:`~gi.repository.Gtk.Label.set_mnemonic_widget` is not called, then the first activatable ancestor of the ``GtkLabel`` will be chosen as the mnemonic widget. For instance, if the label is inside a button or menu item, the button or menu item will automatically become the mnemonic widget and be activated by the mnemonic. :param str: The text of the label, with an underscore in front of the mnemonic character Methods ------- .. rst-class:: interim-class .. class:: Label :no-index: .. method:: get_attributes() -> ~gi.repository.Pango.AttrList | None Gets the label's attribute list. This is the :obj:`~gi.repository.Pango.AttrList` that was set on the label using :obj:`~gi.repository.Gtk.Label.set_attributes`, if any. This function does not reflect attributes that come from the label's markup (see :obj:`~gi.repository.Gtk.Label.set_markup`). If you want to get the effective attributes for the label, use `pango_layout_get_attributes (gtk_label_get_layout (self))`. .. method:: get_current_uri() -> str | None Returns the URI for the currently active link in the label. The active link is the one under the mouse pointer or, in a selectable label, the link in which the text cursor is currently positioned. This function is intended for use in a :obj:`~gi.repository.Gtk.Label.signals.activate_link` handler or for use in a :obj:`~gi.repository.Gtk.Widget.signals.query_tooltip` handler. .. method:: get_ellipsize() -> ~gi.repository.Pango.EllipsizeMode Returns the ellipsizing position of the label. See :obj:`~gi.repository.Gtk.Label.set_ellipsize`. .. method:: get_extra_menu() -> ~gi.repository.Gio.MenuModel | None Gets the extra menu model of ``label``. See :obj:`~gi.repository.Gtk.Label.set_extra_menu`. .. method:: get_justify() -> ~gi.repository.Gtk.Justification Returns the justification of the label. See :obj:`~gi.repository.Gtk.Label.set_justify`. .. method:: get_label() -> str Fetches the text from a label. The returned text includes any embedded underlines indicating mnemonics and Pango markup. (See :obj:`~gi.repository.Gtk.Label.get_text`). .. method:: get_layout() -> ~gi.repository.Pango.Layout Gets the ``PangoLayout`` used to display the label. The layout is useful to e.g. convert text positions to pixel positions, in combination with :obj:`~gi.repository.Gtk.Label.get_layout_offsets`. The returned layout is owned by the ``label`` so need not be freed by the caller. The ``label`` is free to recreate its layout at any time, so it should be considered read-only. .. method:: get_layout_offsets() -> tuple[int, int] Obtains the coordinates where the label will draw its ``PangoLayout``. The coordinates are useful to convert mouse events into coordinates inside the :obj:`~gi.repository.Pango.Layout`, e.g. to take some action if some part of the label is clicked. Remember when using the :obj:`~gi.repository.Pango.Layout` functions you need to convert to and from pixels using :func:`~gi.repository.Pango.PIXELS` or :obj:`~gi.repository.Pango.SCALE`. .. method:: get_lines() -> int Gets the number of lines to which an ellipsized, wrapping label should be limited. See :obj:`~gi.repository.Gtk.Label.set_lines`. .. method:: get_max_width_chars() -> int Retrieves the desired maximum width of ``label``, in characters. See :obj:`~gi.repository.Gtk.Label.set_width_chars`. .. method:: get_mnemonic_keyval() -> int Return the mnemonic accelerator. If the label has been set so that it has a mnemonic key this function returns the keyval used for the mnemonic accelerator. If there is no mnemonic set up it returns ``GDK_KEY_VoidSymbol``. .. method:: get_mnemonic_widget() -> ~gi.repository.Gtk.Widget | None Retrieves the target of the mnemonic (keyboard shortcut) of this label. See :obj:`~gi.repository.Gtk.Label.set_mnemonic_widget`. .. method:: get_natural_wrap_mode() -> ~gi.repository.Gtk.NaturalWrapMode Returns line wrap mode used by the label. See :obj:`~gi.repository.Gtk.Label.set_natural_wrap_mode`. .. versionadded:: 4.6 .. method:: get_selectable() -> bool Returns whether the label is selectable. .. method:: get_selection_bounds() -> tuple[bool, int, int] Gets the selected range of characters in the label. .. method:: get_single_line_mode() -> bool Returns whether the label is in single line mode. .. method:: get_tabs() -> ~gi.repository.Pango.TabArray | None Gets the tabs for ``self``. The returned array will be :const:`None` if “standard” (8-space) tabs are used. Free the return value with :obj:`~gi.repository.Pango.TabArray.free`. .. versionadded:: 4.8 .. method:: get_text() -> str Fetches the text from a label. The returned text is as it appears on screen. This does not include any embedded underlines indicating mnemonics or Pango markup. (See :obj:`~gi.repository.Gtk.Label.get_label`) .. method:: get_use_markup() -> bool Returns whether the label’s text is interpreted as Pango markup. See :obj:`~gi.repository.Gtk.Label.set_use_markup`. .. method:: get_use_underline() -> bool Returns whether an embedded underlines in the label indicate mnemonics. See :obj:`~gi.repository.Gtk.Label.set_use_underline`. .. method:: get_width_chars() -> int Retrieves the desired width of ``label``, in characters. See :obj:`~gi.repository.Gtk.Label.set_width_chars`. .. method:: get_wrap() -> bool Returns whether lines in the label are automatically wrapped. See :obj:`~gi.repository.Gtk.Label.set_wrap`. .. method:: get_wrap_mode() -> ~gi.repository.Pango.WrapMode Returns line wrap mode used by the label. See :obj:`~gi.repository.Gtk.Label.set_wrap_mode`. .. method:: get_xalign() -> float Gets the ``xalign`` of the label. See the :obj:`~gi.repository.Gtk.Label.props.xalign` property. .. method:: get_yalign() -> float Gets the ``yalign`` of the label. See the :obj:`~gi.repository.Gtk.Label.props.yalign` property. .. method:: select_region(start_offset: int, end_offset: int) -> None Selects a range of characters in the label, if the label is selectable. See :obj:`~gi.repository.Gtk.Label.set_selectable`. If the label is not selectable, this function has no effect. If ``start_offset`` or ``end_offset`` are -1, then the end of the label will be substituted. :param start_offset: start offset (in characters not bytes) :param end_offset: end offset (in characters not bytes) .. method:: set_attributes(attrs: ~gi.repository.Pango.AttrList | None = None) -> None Apply attributes to the label text. The attributes set with this function will be applied and merged with any other attributes previously effected by way of the :obj:`~gi.repository.Gtk.Label.props.use_underline` or :obj:`~gi.repository.Gtk.Label.props.use_markup` properties. While it is not recommended to mix markup strings with manually set attributes, if you must; know that the attributes will be applied to the label after the markup string is parsed. :param attrs: a :obj:`~gi.repository.Pango.AttrList` .. method:: set_ellipsize(mode: ~gi.repository.Pango.EllipsizeMode) -> None Sets the mode used to ellipsize the text. The text will be ellipsized if there is not enough space to render the entire string. :param mode: a ``PangoEllipsizeMode`` .. method:: set_extra_menu(model: ~gi.repository.Gio.MenuModel | None = None) -> None Sets a menu model to add when constructing the context menu for ``label``. :param model: a ``GMenuModel`` .. method:: set_justify(jtype: ~gi.repository.Gtk.Justification) -> None Sets the alignment of the lines in the text of the label relative to each other. :const:`~gi.repository.Gtk.Justification.LEFT` is the default value when the widget is first created with :obj:`~gi.repository.Gtk.Label.new`. If you instead want to set the alignment of the label as a whole, use :obj:`~gi.repository.Gtk.Widget.set_halign` instead. :obj:`~gi.repository.Gtk.Label.set_justify` has no effect on labels containing only a single line. :param jtype: a ``GtkJustification`` .. method:: set_label(str: str) -> None Sets the text of the label. The label is interpreted as including embedded underlines and/or Pango markup depending on the values of the :obj:`~gi.repository.Gtk.Label.props.use_underline` and :obj:`~gi.repository.Gtk.Label.props.use_markup` properties. :param str: the new text to set for the label .. method:: set_lines(lines: int) -> None Sets the number of lines to which an ellipsized, wrapping label should be limited. This has no effect if the label is not wrapping or ellipsized. Set this to -1 if you don’t want to limit the number of lines. :param lines: the desired number of lines, or -1 .. method:: set_markup(str: str) -> None Sets the labels text and attributes from markup. The string must be marked up with Pango markup (see :obj:`~gi.repository.Pango.parse_markup`). If the ``str`` is external data, you may need to escape it with :func:`~gi.repository.GLib.markup_escape_text` or :func:`~gi.repository.GLib.markup_printf_escaped`: .. code-block:: c :dedent: GtkWidget *self = gtk_label_new (NULL); const char *str = "..."; const char *format = "\``%s``"; char *markup; markup = g_markup_printf_escaped (format, str); gtk_label_set_markup (GTK_LABEL (self), markup); g_free (markup); This function will set the :obj:`~gi.repository.Gtk.Label.props.use_markup` property to :const:`True` as a side effect. If you set the label contents using the :obj:`~gi.repository.Gtk.Label.props.label` property you should also ensure that you set the :obj:`~gi.repository.Gtk.Label.props.use_markup` property accordingly. See also: :obj:`~gi.repository.Gtk.Label.set_text` :param str: a markup string .. method:: set_markup_with_mnemonic(str: str) -> None Sets the labels text, attributes and mnemonic from markup. Parses ``str`` which is marked up with Pango markup (see :obj:`~gi.repository.Pango.parse_markup`), setting the label’s text and attribute list based on the parse results. If characters in ``str`` are preceded by an underscore, they are underlined indicating that they represent a keyboard accelerator called a mnemonic. The mnemonic key can be used to activate another widget, chosen automatically, or explicitly using :obj:`~gi.repository.Gtk.Label.set_mnemonic_widget`. :param str: a markup string .. method:: set_max_width_chars(n_chars: int) -> None Sets the desired maximum width in characters of ``label`` to ``n_chars``. :param n_chars: the new desired maximum width, in characters. .. method:: set_mnemonic_widget(widget: ~gi.repository.Gtk.Widget | None = None) -> None Associate the label with its mnemonic target. If the label has been set so that it has a mnemonic key (using i.e. :obj:`~gi.repository.Gtk.Label.set_markup_with_mnemonic`, :obj:`~gi.repository.Gtk.Label.set_text_with_mnemonic`, :obj:`~gi.repository.Gtk.Label.new_with_mnemonic` or the :obj:`~gi.repository.Gtk.Label.props.use_underline` property) the label can be associated with a widget that is the target of the mnemonic. When the label is inside a widget (like a :obj:`~gi.repository.Gtk.Button` or a :obj:`~gi.repository.Gtk.Notebook` tab) it is automatically associated with the correct widget, but sometimes (i.e. when the target is a :obj:`~gi.repository.Gtk.Entry` next to the label) you need to set it explicitly using this function. The target widget will be accelerated by emitting the :obj:`~gi.repository.Gtk.Widget.signals.mnemonic_activate` signal on it. The default handler for this signal will activate the widget if there are no mnemonic collisions and toggle focus between the colliding widgets otherwise. :param widget: the target ``GtkWidget``, or :const:`None` to unset .. method:: set_natural_wrap_mode(wrap_mode: ~gi.repository.Gtk.NaturalWrapMode) -> None Select the line wrapping for the natural size request. This only affects the natural size requested, for the actual wrapping used, see the :obj:`~gi.repository.Gtk.Label.props.wrap_mode` property. .. versionadded:: 4.6 :param wrap_mode: the line wrapping mode .. method:: set_selectable(setting: bool) -> None Makes text in the label selectable. Selectable labels allow the user to select text from the label, for copy-and-paste. :param setting: :const:`True` to allow selecting text in the label .. method:: set_single_line_mode(single_line_mode: bool) -> None Sets whether the label is in single line mode. :param single_line_mode: :const:`True` if the label should be in single line mode .. method:: set_tabs(tabs: ~gi.repository.Pango.TabArray | None = None) -> None Sets the default tab stops for paragraphs in ``self``. .. versionadded:: 4.8 :param tabs: tabs as a ``PangoTabArray`` .. method:: set_text(str: str) -> None Sets the text within the ``GtkLabel`` widget. It overwrites any text that was there before. This function will clear any previously set mnemonic accelerators, and set the :obj:`~gi.repository.Gtk.Label.props.use_underline` property to :const:`False` as a side effect. This function will set the :obj:`~gi.repository.Gtk.Label.props.use_markup` property to :const:`False` as a side effect. See also: :obj:`~gi.repository.Gtk.Label.set_markup` :param str: The text you want to set .. method:: set_text_with_mnemonic(str: str) -> None Sets the label’s text from the string ``str``. If characters in ``str`` are preceded by an underscore, they are underlined indicating that they represent a keyboard accelerator called a mnemonic. The mnemonic key can be used to activate another widget, chosen automatically, or explicitly using :obj:`~gi.repository.Gtk.Label.set_mnemonic_widget`. :param str: a string .. method:: set_use_markup(setting: bool) -> None Sets whether the text of the label contains markup. See :obj:`~gi.repository.Gtk.Label.set_markup`. :param setting: :const:`True` if the label’s text should be parsed for markup. .. method:: set_use_underline(setting: bool) -> None Sets whether underlines in the text indicate mnemonics. :param setting: :const:`True` if underlines in the text indicate mnemonics .. method:: set_width_chars(n_chars: int) -> None Sets the desired width in characters of ``label`` to ``n_chars``. :param n_chars: the new desired width, in characters. .. method:: set_wrap(wrap: bool) -> None Toggles line wrapping within the ``GtkLabel`` widget. :const:`True` makes it break lines if text exceeds the widget’s size. :const:`False` lets the text get cut off by the edge of the widget if it exceeds the widget size. Note that setting line wrapping to :const:`True` does not make the label wrap at its parent container’s width, because GTK widgets conceptually can’t make their requisition depend on the parent container’s size. For a label that wraps at a specific position, set the label’s width using :obj:`~gi.repository.Gtk.Widget.set_size_request`. :param wrap: the setting .. method:: set_wrap_mode(wrap_mode: ~gi.repository.Pango.WrapMode) -> None Controls how line wrapping is done. This only affects the label if line wrapping is on. (See :obj:`~gi.repository.Gtk.Label.set_wrap`) The default is ``%PANGO_WRAP_WORD`` which means wrap on word boundaries. For sizing behavior, also consider the :obj:`~gi.repository.Gtk.Label.props.natural_wrap_mode` property. :param wrap_mode: the line wrapping mode .. method:: set_xalign(xalign: float) -> None Sets the ``xalign`` of the label. See the :obj:`~gi.repository.Gtk.Label.props.xalign` property. :param xalign: the new xalign value, between 0 and 1 .. method:: set_yalign(yalign: float) -> None Sets the ``yalign`` of the label. See the :obj:`~gi.repository.Gtk.Label.props.yalign` property. :param yalign: the new yalign value, between 0 and 1 Properties ---------- .. rst-class:: interim-class .. class:: Label :no-index: .. attribute:: props.attributes :type: ~gi.repository.Pango.AttrList A list of style attributes to apply to the text of the label. .. attribute:: props.ellipsize :type: ~gi.repository.Pango.EllipsizeMode The preferred place to ellipsize the string, if the label does not have enough room to display the entire string. Note that setting this property to a value other than ``%PANGO_ELLIPSIZE_NONE`` has the side-effect that the label requests only enough space to display the ellipsis "...". In particular, this means that ellipsizing labels do not work well in notebook tabs, unless the :obj:`~gi.repository.Gtk.NotebookPage.props.tab_expand` child property is set to :const:`True`. Other ways to set a label's width are :obj:`~gi.repository.Gtk.Widget.set_size_request` and :obj:`~gi.repository.Gtk.Label.set_width_chars`. .. attribute:: props.extra_menu :type: ~gi.repository.Gio.MenuModel A menu model whose contents will be appended to the context menu. .. attribute:: props.justify :type: ~gi.repository.Gtk.Justification The alignment of the lines in the text of the label, relative to each other. This does *not* affect the alignment of the label within its allocation. See :obj:`~gi.repository.Gtk.Label.props.xalign` for that. .. attribute:: props.label :type: str The contents of the label. If the string contains Pango markup (see :obj:`~gi.repository.Pango.parse_markup`), you will have to set the :obj:`~gi.repository.Gtk.Label.props.use_markup` property to :const:`True` in order for the label to display the markup attributes. See also :obj:`~gi.repository.Gtk.Label.set_markup` for a convenience function that sets both this property and the :obj:`~gi.repository.Gtk.Label.props.use_markup` property at the same time. If the string contains underlines acting as mnemonics, you will have to set the :obj:`~gi.repository.Gtk.Label.props.use_underline` property to :const:`True` in order for the label to display them. .. attribute:: props.lines :type: int The number of lines to which an ellipsized, wrapping label should be limited. This property has no effect if the label is not wrapping or ellipsized. Set this property to -1 if you don't want to limit the number of lines. .. attribute:: props.max_width_chars :type: int The desired maximum width of the label, in characters. If this property is set to -1, the width will be calculated automatically. See the section on `text layout `_ for details of how :obj:`~gi.repository.Gtk.Label.props.width_chars` and :obj:`~gi.repository.Gtk.Label.props.max_width_chars` determine the width of ellipsized and wrapped labels. .. attribute:: props.mnemonic_keyval :type: int The mnemonic accelerator key for the label. .. attribute:: props.mnemonic_widget :type: ~gi.repository.Gtk.Widget The widget to be activated when the labels mnemonic key is pressed. .. attribute:: props.natural_wrap_mode :type: ~gi.repository.Gtk.NaturalWrapMode Select the line wrapping for the natural size request. This only affects the natural size requested. For the actual wrapping used, see the :obj:`~gi.repository.Gtk.Label.props.wrap_mode` property. The default is :const:`~gi.repository.Gtk.NaturalWrapMode.INHERIT`, which inherits the behavior of the :obj:`~gi.repository.Gtk.Label.props.wrap_mode` property. .. versionadded:: 4.6 .. attribute:: props.selectable :type: bool Whether the label text can be selected with the mouse. .. attribute:: props.single_line_mode :type: bool Whether the label is in single line mode. In single line mode, the height of the label does not depend on the actual text, it is always set to ascent + descent of the font. This can be an advantage in situations where resizing the label because of text changes would be distracting, e.g. in a statusbar. .. attribute:: props.tabs :type: ~gi.repository.Pango.TabArray Custom tabs for this label. .. versionadded:: 4.8 .. attribute:: props.use_markup :type: bool :const:`True` if the text of the label includes Pango markup. See :obj:`~gi.repository.Pango.parse_markup`. .. attribute:: props.use_underline :type: bool :const:`True` if the text of the label indicates a mnemonic with an _ before the mnemonic character. .. attribute:: props.width_chars :type: int The desired width of the label, in characters. If this property is set to -1, the width will be calculated automatically. See the section on `text layout `_ for details of how :obj:`~gi.repository.Gtk.Label.props.width_chars` and :obj:`~gi.repository.Gtk.Label.props.max_width_chars` determine the width of ellipsized and wrapped labels. .. attribute:: props.wrap :type: bool :const:`True` if the label text will wrap if it gets too wide. .. attribute:: props.wrap_mode :type: ~gi.repository.Pango.WrapMode Controls how the line wrapping is done. This only affects the formatting if line wrapping is on (see the :obj:`~gi.repository.Gtk.Label.props.wrap` property). The default is ``%PANGO_WRAP_WORD``, which means wrap on word boundaries. For sizing behavior, also consider the :obj:`~gi.repository.Gtk.Label.props.natural_wrap_mode` property. .. attribute:: props.xalign :type: float The horizontal alignment of the label text inside its size allocation. Compare this to :obj:`~gi.repository.Gtk.Widget.props.halign`, which determines how the labels size allocation is positioned in the space available for the label. .. attribute:: props.yalign :type: float The vertical alignment of the label text inside its size allocation. Compare this to :obj:`~gi.repository.Gtk.Widget.props.valign`, which determines how the labels size allocation is positioned in the space available for the label. Signals ------- .. rst-class:: interim-class .. class:: Label.signals :no-index: .. method:: activate_current_link() -> None Gets emitted when the user activates a link in the label. The ::activate-current-link is a `keybinding signal `_. Applications may also emit the signal with :func:`~gi.repository.GObject.signal_emit_by_name` if they need to control activation of URIs programmatically. The default bindings for this signal are all forms of the :kbd:`Enter` key. .. method:: activate_link(uri: str) -> bool Gets emitted to activate a URI. Applications may connect to it to override the default behaviour, which is to call :obj:`~gi.repository.Gtk.FileLauncher.launch`. :param uri: the URI that is activated .. method:: copy_clipboard() -> None Gets emitted to copy the selection to the clipboard. The ::copy-clipboard signal is a `keybinding signal `_. The default binding for this signal is :kbd:`Ctrl`+:kbd:`c`. .. method:: move_cursor(step: ~gi.repository.Gtk.MovementStep, count: int, extend_selection: bool) -> None Gets emitted when the user initiates a cursor movement. The ::move-cursor signal is a `keybinding signal `_. If the cursor is not visible in ``entry``, this signal causes the viewport to be moved instead. Applications should not connect to it, but may emit it with :func:`~gi.repository.GObject.signal_emit_by_name` if they need to control the cursor programmatically. The default bindings for this signal come in two variants, the variant with the Shift modifier extends the selection, the variant without the Shift modifier does not. There are too many key combinations to list them all here. - :kbd:`←`, :kbd:`→`, :kbd:`↑`, :kbd:`↓` move by individual characters/lines - :kbd:`Ctrl`+:kbd:`←`, etc. move by words/paragraphs - :kbd:`Home` and :kbd:`End` move to the ends of the buffer :param step: the granularity of the move, as a ``GtkMovementStep`` :param count: the number of ``step`` units to move :param extend_selection: :const:`True` if the move should extend the selection