:right-sidebar: True Text =================================================================== .. currentmodule:: gi.repository.Gtk .. class:: Text(**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`, :class:`~gi.repository.Gtk.Editable` The ``GtkText`` widget is a single-line text entry widget. ``GtkText`` is the common implementation of single-line text editing that is shared between :obj:`~gi.repository.Gtk.Entry`, :obj:`~gi.repository.Gtk.PasswordEntry`, :obj:`~gi.repository.Gtk.SpinButton`, and other widgets. In all of these, ``GtkText`` is used as the delegate for the :obj:`~gi.repository.Gtk.Editable` implementation. A fairly large set of key bindings are supported by default. If the entered text is longer than the allocation of the widget, the widget will scroll so that the cursor position is visible. When using an entry for passwords and other sensitive information, it can be put into “password mode” using :obj:`~gi.repository.Gtk.Text.set_visibility`. In this mode, entered text is displayed using a “invisible” character. By default, GTK picks the best invisible character that is available in the current font, but it can be changed with :obj:`~gi.repository.Gtk.Text.set_invisible_char`. If you are looking to add icons or progress display in an entry, look at :obj:`~gi.repository.Gtk.Entry`. There other alternatives for more specialized use cases, such as :obj:`~gi.repository.Gtk.SearchEntry`. If you need multi-line editable text, look at :obj:`~gi.repository.Gtk.TextView`. CSS nodes --------- .. code-block:: :dedent: text[.read-only] ├── placeholder ├── undershoot.left ├── undershoot.right ├── [selection] ├── [block-cursor] ╰── [window.popup] ``GtkText`` has a main node with the name ``text``. Depending on the properties of the widget, the ``.read-only`` style class may appear. When the entry has a selection, it adds a subnode with the name ``selection``. When the entry is in overwrite mode, it adds a subnode with the name ``block-cursor`` that determines how the block cursor is drawn. The CSS node for a context menu is added as a subnode with the name ``popup``. The ``undershoot`` nodes are used to draw the underflow indication when content is scrolled out of view. These nodes get the ``.left`` or ``.right`` style class added depending on where the indication is drawn. When touch is used and touch selection handles are shown, they are using CSS nodes with name ``cursor-handle``. They get the ``.top`` or ``.bottom`` style class depending on where they are shown in relation to the selection. If there is just a single handle for the text cursor, it gets the style class ``.insertion-cursor``. Accessibility ------------- ``GtkText`` uses the :const:`~gi.repository.Gtk.AccessibleRole.NONE` role, which causes it to be skipped for accessibility. This is because ``GtkText`` is expected to be used as a delegate for a ``GtkEditable`` implementation that will be represented to accessibility. Constructors ------------ .. rst-class:: interim-class .. class:: Text :no-index: .. classmethod:: new() -> ~gi.repository.Gtk.Widget Creates a new ``GtkText``. .. classmethod:: new_with_buffer(buffer: ~gi.repository.Gtk.EntryBuffer) -> ~gi.repository.Gtk.Widget Creates a new ``GtkText`` with the specified text buffer. :param buffer: The buffer to use for the new ``GtkText``. Methods ------- .. rst-class:: interim-class .. class:: Text :no-index: .. method:: compute_cursor_extents(position: int) -> tuple[~gi.repository.Graphene.Rect, ~gi.repository.Graphene.Rect] Determine the positions of the strong and weak cursors if the insertion point in the layout is at ``position``. The position of each cursor is stored as a zero-width rectangle. The strong cursor location is the location where characters of the directionality equal to the base direction are inserted. The weak cursor location is the location where characters of the directionality opposite to the base direction are inserted. The rectangle positions are in widget coordinates. .. versionadded:: 4.4 :param position: the character position .. method:: get_activates_default() -> bool Returns whether pressing Enter will activate the default widget for the window containing ``self``. See :obj:`~gi.repository.Gtk.Text.set_activates_default`. .. method:: get_attributes() -> ~gi.repository.Pango.AttrList | None Gets the attribute list that was set on the ``GtkText``. See :obj:`~gi.repository.Gtk.Text.set_attributes`. .. method:: get_buffer() -> ~gi.repository.Gtk.EntryBuffer Get the ``GtkEntryBuffer`` object which holds the text for this widget. .. method:: get_enable_emoji_completion() -> bool Returns whether Emoji completion is enabled for this ``GtkText`` widget. .. method:: get_extra_menu() -> ~gi.repository.Gio.MenuModel | None Gets the menu model for extra items in the context menu. See :obj:`~gi.repository.Gtk.Text.set_extra_menu`. .. method:: get_input_hints() -> ~gi.repository.Gtk.InputHints Gets the input hints of the ``GtkText``. .. method:: get_input_purpose() -> ~gi.repository.Gtk.InputPurpose Gets the input purpose of the ``GtkText``. .. method:: get_invisible_char() -> str Retrieves the character displayed when visibility is set to false. Note that GTK does not compute this value unless it needs it, so the value returned by this function is not very useful unless it has been explicitly set with :obj:`~gi.repository.Gtk.Text.set_invisible_char`. .. method:: get_max_length() -> int Retrieves the maximum allowed length of the text in ``self``. See :obj:`~gi.repository.Gtk.Text.set_max_length`. This is equivalent to getting ``self``'s ``GtkEntryBuffer`` and calling :obj:`~gi.repository.Gtk.EntryBuffer.get_max_length` on it. .. method:: get_overwrite_mode() -> bool Gets whether text is overwritten when typing in the ``GtkText``. See :obj:`~gi.repository.Gtk.Text.set_overwrite_mode`. .. method:: get_placeholder_text() -> str | None Retrieves the text that will be displayed when ``self`` is empty and unfocused If no placeholder text has been set, :const:`None` will be returned. .. method:: get_propagate_text_width() -> bool Returns whether the ``GtkText`` will grow and shrink with the content. .. method:: get_tabs() -> ~gi.repository.Pango.TabArray | None Gets the tabstops that were set on the ``GtkText``. See :obj:`~gi.repository.Gtk.Text.set_tabs`. .. method:: get_text_length() -> int Retrieves the current length of the text in ``self``. This is equivalent to getting ``self``'s ``GtkEntryBuffer`` and calling :obj:`~gi.repository.Gtk.EntryBuffer.get_length` on it. .. method:: get_truncate_multiline() -> bool Returns whether the ``GtkText`` will truncate multi-line text that is pasted into the widget .. method:: get_visibility() -> bool Retrieves whether the text in ``self`` is visible. .. method:: grab_focus_without_selecting() -> bool Causes ``self`` to have keyboard focus. It behaves like :obj:`~gi.repository.Gtk.Widget.grab_focus`, except that it doesn't select the contents of ``self``. You only want to call this on some special entries which the user usually doesn't want to replace all text in, such as search-as-you-type entries. .. method:: set_activates_default(activates: bool) -> None If ``activates`` is :const:`True`, pressing Enter will activate the default widget for the window containing ``self``. This usually means that the dialog containing the ``GtkText`` will be closed, since the default widget is usually one of the dialog buttons. :param activates: :const:`True` to activate window’s default widget on Enter keypress .. method:: set_attributes(attrs: ~gi.repository.Pango.AttrList | None = None) -> None Sets attributes that are applied to the text. :param attrs: a ``PangoAttrList`` .. method:: set_buffer(buffer: ~gi.repository.Gtk.EntryBuffer) -> None Set the ``GtkEntryBuffer`` object which holds the text for this widget. :param buffer: a ``GtkEntryBuffer`` .. method:: set_enable_emoji_completion(enable_emoji_completion: bool) -> None Sets whether Emoji completion is enabled. If it is, typing ':', followed by a recognized keyword, will pop up a window with suggested Emojis matching the keyword. :param enable_emoji_completion: :const:`True` to enable Emoji completion .. method:: set_extra_menu(model: ~gi.repository.Gio.MenuModel | None = None) -> None Sets a menu model to add when constructing the context menu for ``self``. :param model: a ``GMenuModel`` .. method:: set_input_hints(hints: ~gi.repository.Gtk.InputHints) -> None Sets input hints that allow input methods to fine-tune their behaviour. :param hints: the hints .. method:: set_input_purpose(purpose: ~gi.repository.Gtk.InputPurpose) -> None Sets the input purpose of the ``GtkText``. This can be used by on-screen keyboards and other input methods to adjust their behaviour. :param purpose: the purpose .. method:: set_invisible_char(ch: str) -> None Sets the character to use when in “password mode”. By default, GTK picks the best invisible char available in the current font. If you set the invisible char to 0, then the user will get no feedback at all; there will be no text on the screen as they type. :param ch: a Unicode character .. method:: set_max_length(length: int) -> None Sets the maximum allowed length of the contents of the widget. If the current contents are longer than the given length, then they will be truncated to fit. This is equivalent to getting ``self``'s ``GtkEntryBuffer`` and calling :obj:`~gi.repository.Gtk.EntryBuffer.set_max_length` on it. :param length: the maximum length of the ``GtkText``, or 0 for no maximum. (other than the maximum length of entries.) The value passed in will be clamped to the range 0-65536. .. method:: set_overwrite_mode(overwrite: bool) -> None Sets whether the text is overwritten when typing in the ``GtkText``. :param overwrite: new value .. method:: set_placeholder_text(text: str | None = None) -> None Sets text to be displayed in ``self`` when it is empty. This can be used to give a visual hint of the expected contents of the ``GtkText``. :param text: a string to be displayed when ``self`` is empty and unfocused .. method:: set_propagate_text_width(propagate_text_width: bool) -> None Sets whether the ``GtkText`` should grow and shrink with the content. :param propagate_text_width: :const:`True` to propagate the text width .. method:: set_tabs(tabs: ~gi.repository.Pango.TabArray | None = None) -> None Sets tabstops that are applied to the text. :param tabs: a ``PangoTabArray`` .. method:: set_truncate_multiline(truncate_multiline: bool) -> None Sets whether the ``GtkText`` should truncate multi-line text that is pasted into the widget. :param truncate_multiline: :const:`True` to truncate multi-line text .. method:: set_visibility(visible: bool) -> None Sets whether the contents of the ``GtkText`` are visible or not. When visibility is set to :const:`False`, characters are displayed as the invisible char, and will also appear that way when the text in the widget is copied to the clipboard. By default, GTK picks the best invisible character available in the current font, but it can be changed with :obj:`~gi.repository.Gtk.Text.set_invisible_char`. Note that you probably want to set :obj:`~gi.repository.Gtk.Text.props.input_purpose` to :const:`~gi.repository.Gtk.InputPurpose.PASSWORD` or :const:`~gi.repository.Gtk.InputPurpose.PIN` to inform input methods about the purpose of this self, in addition to setting visibility to :const:`False`. :param visible: :const:`True` if the contents of the ``GtkText`` are displayed as plaintext .. method:: unset_invisible_char() -> None Unsets the invisible char. After calling this, the default invisible char is used again. Properties ---------- .. rst-class:: interim-class .. class:: Text :no-index: .. attribute:: props.activates_default :type: bool Whether to activate the default widget when Enter is pressed. .. attribute:: props.attributes :type: ~gi.repository.Pango.AttrList A list of Pango attributes to apply to the text of the ``GtkText``. This is mainly useful to change the size or weight of the text. The ``PangoAttribute``'s ``start_index`` and ``end_index`` must refer to the ``GtkEntryBuffer`` text, i.e. without the preedit string. .. attribute:: props.buffer :type: ~gi.repository.Gtk.EntryBuffer The ``GtkEntryBuffer`` object which stores the text. .. attribute:: props.enable_emoji_completion :type: bool Whether to suggest Emoji replacements. .. attribute:: props.extra_menu :type: ~gi.repository.Gio.MenuModel A menu model whose contents will be appended to the context menu. .. attribute:: props.im_module :type: str Which IM (input method) module should be used for this self. See :obj:`~gi.repository.Gtk.IMMulticontext`. Setting this to a non-:const:`None` value overrides the system-wide IM module setting. See the :obj:`~gi.repository.Gtk.Settings.props.gtk_im_module` property. .. attribute:: props.input_hints :type: ~gi.repository.Gtk.InputHints Additional hints that allow input methods to fine-tune their behaviour. .. attribute:: props.input_purpose :type: ~gi.repository.Gtk.InputPurpose The purpose of this text field. This property can be used by on-screen keyboards and other input methods to adjust their behaviour. Note that setting the purpose to :const:`~gi.repository.Gtk.InputPurpose.PASSWORD` or :const:`~gi.repository.Gtk.InputPurpose.PIN` is independent from setting :obj:`~gi.repository.Gtk.Text.props.visibility`. .. attribute:: props.invisible_char :type: int The character to used when masking contents (in “password mode”). .. attribute:: props.invisible_char_set :type: bool Whether the invisible char has been set for the ``GtkText``. .. attribute:: props.max_length :type: int Maximum number of characters that are allowed. Zero indicates no limit. .. attribute:: props.overwrite_mode :type: bool If text is overwritten when typing in the ``GtkText``. .. attribute:: props.placeholder_text :type: str The text that will be displayed in the ``GtkText`` when it is empty and unfocused. .. attribute:: props.propagate_text_width :type: bool Whether the widget should grow and shrink with the content. .. attribute:: props.scroll_offset :type: int Number of pixels scrolled of the screen to the left. .. attribute:: props.tabs :type: ~gi.repository.Pango.TabArray A list of tabstops to apply to the text of the ``GtkText``. .. attribute:: props.truncate_multiline :type: bool When :const:`True`, pasted multi-line text is truncated to the first line. .. attribute:: props.visibility :type: bool If :const:`False`, the text is masked with the “invisible char”. Signals ------- .. rst-class:: interim-class .. class:: Text.signals :no-index: .. method:: activate() -> None Emitted when the user hits the :kbd:`Enter` key. The default bindings for this signal are all forms of the :kbd:`Enter` key. .. method:: backspace() -> None Emitted when the user asks for it. This is a `keybinding signal `_. The default bindings for this signal are :kbd:`Backspace` and :kbd:`Shift`+:kbd:`Backspace`. .. method:: copy_clipboard() -> None Emitted to copy the selection to the clipboard. This is a `keybinding signal `_. The default bindings for this signal are :kbd:`Ctrl`+:kbd:`c` and :kbd:`Ctrl`+:kbd:`Insert`. .. method:: cut_clipboard() -> None Emitted to cut the selection to the clipboard. This is a `keybinding signal `_. The default bindings for this signal are :kbd:`Ctrl`+:kbd:`x` and :kbd:`Shift`+:kbd:`Delete`. .. method:: delete_from_cursor(type: ~gi.repository.Gtk.DeleteType, count: int) -> None Emitted when the user initiates a text deletion. This is a `keybinding signal `_. If the ``type`` is :const:`~gi.repository.Gtk.DeleteType.CHARS`, GTK deletes the selection if there is one, otherwise it deletes the requested number of characters. The default bindings for this signal are :kbd:`Delete` for deleting a character and :kbd:`Ctrl`+:kbd:`Delete` for deleting a word. :param type: the granularity of the deletion, as a ``GtkDeleteType`` :param count: the number of ``type`` units to delete .. method:: insert_at_cursor(string: str) -> None Emitted when the user initiates the insertion of a fixed string at the cursor. This is a `keybinding signal `_. This signal has no default bindings. :param string: the string to insert .. method:: insert_emoji() -> None Emitted to present the Emoji chooser for the widget. This is a `keybinding signal `_. The default bindings for this signal are :kbd:`Ctrl`+. and :kbd:`Ctrl`+; .. method:: move_cursor(step: ~gi.repository.Gtk.MovementStep, count: int, extend: bool) -> None Emitted when the user initiates a cursor movement. If the cursor is not visible in ``self``, this signal causes the viewport to be moved instead. This is a `keybinding signal `_. 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 :kbd:`Shift` modifier extends the selection, the variant without it 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: :const:`True` if the move should extend the selection .. method:: paste_clipboard() -> None Emitted to paste the contents of the clipboard. This is a `keybinding signal `_. The default bindings for this signal are :kbd:`Ctrl`+:kbd:`v` and :kbd:`Shift`+:kbd:`Insert`. .. method:: preedit_changed(preedit: str) -> None Emitted when the preedit text changes. If an input method is used, the typed text will not immediately be committed to the buffer. So if you are interested in the text, connect to this signal. :param preedit: the current preedit string .. method:: toggle_overwrite() -> None Emitted to toggle the overwrite mode of the ``GtkText``. This is a `keybinding signal `_. The default bindings for this signal is :kbd:`Insert`. Fields ------ .. rst-class:: interim-class .. class:: Text :no-index: .. attribute:: parent_instance