Debian Codegraph @

docs/tutorials/gtk4/display-widgets/label.rst

raw
1 .. currentmodule:: gi.repository
2
3 Label
4 =====
5 Labels are the main method of placing non-editable text in windows, for instance
6 to place a title next to a :class:`Gtk.Entry` widget. You can specify the text
7 in the constructor, or later with the :attr:`Gtk.Label.props.label` property.
8
9 The width of the label will be adjusted automatically. You can produce
10 multi-line labels by putting line breaks (``\n``) in the label string.
11
12 Labels can be made selectable with :attr:`Gtk.Label.props.selectable`.
13 Selectable labels allow the user to copy the label contents to the
14 clipboard. Only labels that contain useful-to-copy information —
15 such as error messages — should be made selectable.
16
17 The label text can be justified using the :attr:`Gtk.Label.props.justify` prop.
18 The widget is also capable of word-wrapping, which can be activated with
19 :attr:`Gtk.Label.props.wrap`. You can configure this wrapping by changing
20 :attr:`Gtk.Label.props.wrap_mode`, in can be :attr:`Pango.WrapMode.WORD`,
21 :attr:`Pango.WrapMode.CHAR` or :attr:`Pango.WrapMode.WORD_CHAR`.
22
23 :class:`Gtk.Label` support some simple formatting, for instance allowing you to
24 make some text bold, colored, or larger. You can do this by enabling markup
25 setting :attr:`Gtk.Label.props.use_markup` to ``True``, then you can use markup
26 with :attr:`Gtk.Label.props.label` using the Pango Markup syntax [#pango]_.
27 For instance, ``<b>bold text</b> and <s>strikethrough text</s>``.
28 In addition, :class:`Gtk.Label` supports clickable hyperlinks.
29 The markup for links is borrowed from HTML, using the a with href and title
30 attributes. GTK renders links similar to the way they appear in web browsers,
31 with colored, underlined text. The title attribute is displayed as a tooltip
32 on the link. :meth:`Gtk.Label.set_markup` method can be used to set the text and
33 enable markup at the same time.
34
35 .. code-block:: python
36
37 label.set_markup("Go to <a href=\"https://www.gtk.org\" "
38 "title=\"Our website\">GTK+ website</a> for more")
39
40 Labels may contain *mnemonics*. Mnemonics are underlined characters in the
41 label, used for keyboard navigation. Mnemonics are created by providing a
42 string with an underscore before the mnemonic character, such as "_File",
43 to the functions :meth:`Gtk.Label.new_with_mnemonic` or
44 :meth:`Gtk.Label.set_text_with_mnemonic`.
45 Mnemonics automatically activate any activatable widget the label is inside,
46 such as a :class:`Gtk.Button`; if the label is not inside the mnemonic's target
47 widget, you have to tell the label about the target using
48 :attr:`Gtk.Label.props.mnemonic_widget`.
49
50 You can also change the alignment of the label text inside its size allocation.
51 For example if you use :attr:`Gtk.Widget.props.hexpand` in a label, the label
52 may take more space than its inner text.
53 You can change this alignment using :attr:`Gtk.Label.props.xalign` for
54 horizontal alignment and :attr:`Gtk.Label.props.yalign` for vertical.
55
56 Example
57 -------
58
59 .. image:: images/label.png
60
61 .. literalinclude:: examples/label.py
62 :linenos:
63
64 .. [#pango] Pango Markup Syntax, https://docs.gtk.org/Pango/pango_markup.html