Skip to content

Gtk.Text

class — extends Widget, Accessible, AccessibleText, Buildable, ConstraintTarget, Editable

A single-line text entry.

GtkText is the common implementation of single-line text editing that is shared between Entry, PasswordEntry, SpinButton, and other widgets. In all of these, a GtkText instance is used as the delegate for the Editable implementation.

A large number of key bindings s 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 Text.set_visibility. In this mode, entered text is displayed using an “invisible” character. By default, GTK picks the best invisible character that is available in the current font, but it can be changed with Text.set_invisible_char.

If you want to add icons or progress display in an entry, look at Entry. There are other alternatives for more specialized use cases, such as SearchEntry.

If you need multi-line editable text, use TextView.

Shortcuts and Gestures

GtkText supports the following keyboard shortcuts:

  • <kbd>Shift</kbd>+<kbd>F10</kbd> or <kbd>Menu</kbd> opens the context menu.
  • <kbd>Ctrl</kbd>+<kbd>A</kbd> or <kbd>Ctrl</kbd>+<kbd>/</kbd> selects all the text.
  • <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>A</kbd> or <kbd>Ctrl</kbd>+<kbd>\</kbd> unselects all.
  • <kbd>Ctrl</kbd>+<kbd>Z</kbd> undoes the last modification.
  • <kbd>Ctrl</kbd>+<kbd>Y</kbd> or <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Z</kbd> redoes the last undone modification.
  • <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>T</kbd> toggles the text direction.
  • <kbd>Clear</kbd> clears the content.

Additionally, the following signals have default keybindings:

Actions

GtkText defines a set of built-in actions:

  • clipboard.copy copies the contents to the clipboard.
  • clipboard.cut copies the contents to the clipboard and deletes it from the widget.
  • clipboard.paste inserts the contents of the clipboard into the widget.
  • menu.popup opens the context menu.
  • misc.insert-emoji opens the Emoji chooser.
  • misc.toggle-visibility toggles the GtkText:visibility property.
  • misc.toggle-direction toggles the text direction.
  • selection.delete deletes the current selection.
  • selection.select-all selects all of the widgets content.
  • text.redo redoes the last change to the contents.
  • text.undo undoes the last change to the contents.
  • text.clear removes all content.

CSS nodes

text[.read-only]
├── placeholder
├── undershoot.left
├── undershoot.right
├── [selection]
├── [cursor-handle[.top]
├── [cursor-handle.bottom]
├── [block-cursor]
├── [cursor-handle[.top/.bottom][.insertion-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 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

new

@classmethod
def new(cls) -> Widget

Creates a new GtkText.

new_with_buffer

@classmethod
def new_with_buffer(cls, buffer: EntryBuffer) -> Widget

Creates a new GtkText with the specified buffer.

Parameters:

  • buffer — the buffer to use

Methods

compute_cursor_extents

def compute_cursor_extents(self, position: int) -> tuple[Graphene.Rect, Graphene.Rect]

Determines the positions of the strong and weak cursors for a given character 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.

Parameters:

  • position — the character position

get_activates_default

def get_activates_default(self) -> bool

Returns whether pressing <kbd>Enter</kbd> will activate the default widget for the window containing the widget.

See Text.set_activates_default.

get_attributes

def get_attributes(self) -> Pango.AttrList | None

Gets the attribute list that was set on the text widget.

See Text.set_attributes.

get_buffer

def get_buffer(self) -> EntryBuffer

Get the entry buffer object which holds the text for this widget.

get_enable_emoji_completion

def get_enable_emoji_completion(self) -> bool

Returns whether Emoji completion is enabled.

get_extra_menu

def get_extra_menu(self) -> Gio.MenuModel | None

Gets the extra menu model of the text widget.

See Text.set_extra_menu.

get_input_hints

def get_input_hints(self) -> InputHints

Gets the input hints of the text widget.

get_input_purpose

def get_input_purpose(self) -> InputPurpose

Gets the input purpose of the text widget.

get_invisible_char

def get_invisible_char(self) -> 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 Text.set_invisible_char.

get_max_length

def get_max_length(self) -> int

Retrieves the maximum allowed length of the contents.

See Text.set_max_length.

This is equivalent to getting self's GtkEntryBuffer and calling EntryBuffer.get_max_length on it.

get_overwrite_mode

def get_overwrite_mode(self) -> bool

Gets whether text is overwritten when typing.

See Text.set_overwrite_mode.

get_placeholder_text

def get_placeholder_text(self) -> str | None

Retrieves the text that will be displayed when the text widget is empty and unfocused

See Text.set_placeholder_text.

get_propagate_text_width

def get_propagate_text_width(self) -> bool

Returns whether the text widget will grow and shrink with the content.

get_tabs

def get_tabs(self) -> Pango.TabArray | None

Gets the tab stops for the text widget.

See Text.set_tabs.

get_text_length

def get_text_length(self) -> int

Retrieves the length of the contents.

This is equivalent to getting self's GtkEntryBuffer and calling EntryBuffer.get_length on it.

get_truncate_multiline

def get_truncate_multiline(self) -> bool

Returns whether pasted text will be truncated to the first line.

get_visibility

def get_visibility(self) -> bool

Retrieves whether the text is visible.

grab_focus_without_selecting

def grab_focus_without_selecting(self) -> bool

Causes the text widget to have the keyboard focus.

It behaves like Widget.grab_focus, except that it does not 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.

set_activates_default

def set_activates_default(self, activates: bool) -> None

Sets whether pressing <kbd>Enter</kbd> will activate the default widget.

This usually means that the dialog containing self will be closed, since the default widget is usually one of the dialog buttons.

Parameters:

  • activates — true to activate window’s default widget on <kbd>Enter</kbd> keypress

set_attributes

def set_attributes(self, attrs: Pango.AttrList | None = ...) -> None

Apply attributes to the contents of the text widget.

Parameters:

  • attrs — a list of style attributes

set_buffer

def set_buffer(self, buffer: EntryBuffer) -> None

Set the entry buffer object which holds the text for this widget.

Parameters:

  • buffer — an entry buffer object

set_enable_emoji_completion

def set_enable_emoji_completion(self, 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.

Parameters:

  • enable_emoji_completion — true to enable Emoji completion

set_extra_menu

def set_extra_menu(self, model: Gio.MenuModel | None = ...) -> None

Sets a menu model to add to the context menu of the text widget.

Parameters:

  • model — a menu model

set_input_hints

def set_input_hints(self, hints: InputHints | int) -> None

Sets hints that allow input methods to fine-tune their behaviour.

Parameters:

  • hints — input hints

set_input_purpose

def set_input_purpose(self, purpose: InputPurpose | int) -> None

Sets the input purpose of the text widget.

The input purpose can be used by on-screen keyboards and other input methods to adjust their behaviour.

Parameters:

  • purpose — the input purpose

set_invisible_char

def set_invisible_char(self, 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.

Parameters:

  • ch — a Unicode character

set_max_length

def set_max_length(self, length: int) -> None

Sets the maximum allowed length of the contents.

If the current contents are longer than the given length, they will be truncated to fit.

This is equivalent to getting self's GtkEntryBuffer and calling EntryBuffer.set_max_length on it.

Parameters:

  • length — the maximum length of the text, or 0 for no maximum. (other than the maximum length of entries.) The value passed in will be clamped to the range 0-65536

set_overwrite_mode

def set_overwrite_mode(self, overwrite: bool) -> None

Sets whether the text is overwritten when typing.

Parameters:

  • overwrite — new value

set_placeholder_text

def set_placeholder_text(self, text: str | None = ...) -> None

Sets the text to be displayed when the text widget is empty and unfocused.

This can be used to give a visual hint of the expected contents of the text widget.

Parameters:

  • text — a string to be displayed when self is empty and unfocused

set_propagate_text_width

def set_propagate_text_width(self, propagate_text_width: bool) -> None

Sets whether the text widget should grow and shrink with the content.

Parameters:

  • propagate_text_width — true to propagate the text width

set_tabs

def set_tabs(self, tabs: Pango.TabArray | None = ...) -> None

Sets tab stops for the text widget.

Parameters:

  • tabs — tab stops

set_truncate_multiline

def set_truncate_multiline(self, truncate_multiline: bool) -> None

Sets whether pasted text should be truncated to the first line.

Parameters:

  • truncate_multiline — true to truncate multi-line text

set_visibility

def set_visibility(self, visible: bool) -> None

Sets whether the contents of the text widget are visible or not.

When visibility is set to false, characters are displayed as the invisible char, and it 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 Text.set_invisible_char.

Note that you probably want to set Text.input-purpose to InputPurpose.password or InputPurpose.pin to inform input methods about the purpose of this widget, in addition to setting visibility to false.

Parameters:

  • visible — true if the contents of the text widget are displayed as plain text

unset_invisible_char

def unset_invisible_char(self) -> None

Unsets the invisible char.

After calling this, the default invisible char is used again.

Properties

activates_default

activates_default: bool  # read/write

Whether to activate the default widget when <kbd>Enter</kbd> is pressed.

attributes

attributes: Pango.AttrList  # read/write

A list of Pango attributes to apply to the text.

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.

buffer

buffer: EntryBuffer  # read/write

The GtkEntryBuffer object which stores the text.

enable_emoji_completion

enable_emoji_completion: bool  # read/write

Whether to suggest Emoji replacements.

extra_menu

extra_menu: Gio.MenuModel  # read/write

A menu model whose contents will be appended to the context menu.

im_module

im_module: str  # read/write

Which input method module should be used.

See IMMulticontext.

Setting this to a non-NULL value overrides the system-wide input method. See the Settings.gtk-im-module setting.

input_hints

input_hints: InputHints | int  # read/write

Additional hints that allow input methods to fine-tune their behaviour.

input_purpose

input_purpose: InputPurpose | int  # read/write

The purpose of this text field.

This information can be used by on-screen keyboards and other input methods to adjust their behaviour.

Note that setting the purpose to InputPurpose.password or InputPurpose.pin is independent from setting Text.visibility.

invisible_char

invisible_char: int  # read/write

The character to used when masking contents (in “password mode”).

invisible_char_set

invisible_char_set: bool  # read/write

Whether the invisible char has been set.

max_length

max_length: int  # read/write

Maximum number of characters that are allowed.

Zero indicates no limit.

overwrite_mode

overwrite_mode: bool  # read/write

If text is overwritten when typing.

placeholder_text

placeholder_text: str  # read/write

The text that will be displayed in the GtkText when it is empty and unfocused.

propagate_text_width

propagate_text_width: bool  # read/write

Whether the widget should grow and shrink with the content.

scroll_offset

scroll_offset: int  # read-only

Number of pixels scrolled of the screen to the left.

tabs

tabs: Pango.TabArray  # read/write

Custom tabs for this text widget.

truncate_multiline

truncate_multiline: bool  # read/write

When true, pasted multi-line text is truncated to the first line.

visibility

visibility: bool  # read/write

If false, the text is masked with the “invisible char”.

Signals

activate

def on_activate(self) -> None: ...

Emitted when the user hits the <kbd>Enter</kbd> key.

The default bindings for this signal are all forms of the <kbd>Enter</kbd> key.

backspace

def on_backspace(self) -> None: ...

Emitted when the user asks for it.

This is a keybinding signal.

The default bindings for this signal are <kbd>Backspace</kbd> and <kbd>Shift</kbd>+<kbd>Backspace</kbd>.

copy-clipboard

def on_copy_clipboard(self) -> None: ...

Emitted to copy the selection to the clipboard.

This is a keybinding signal.

The default bindings for this signal are <kbd>Ctrl</kbd>+<kbd>c</kbd> and <kbd>Ctrl</kbd>+<kbd>Insert</kbd>.

cut-clipboard

def on_cut_clipboard(self) -> None: ...

Emitted to cut the selection to the clipboard.

This is a keybinding signal.

The default bindings for this signal are <kbd>Ctrl</kbd>+<kbd>x</kbd> and <kbd>Shift</kbd>+<kbd>Delete</kbd>.

delete-from-cursor

def on_delete_from_cursor(self, type: DeleteType, count: int) -> None: ...

Emitted when the user initiates a text deletion.

This is a keybinding signal.

If the type is 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</kbd> for deleting a character and <kbd>Ctrl</kbd>+<kbd>Delete</kbd> for deleting a word.

insert-at-cursor

def on_insert_at_cursor(self, 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.

insert-emoji

def on_insert_emoji(self) -> None: ...

Emitted to present the Emoji chooser.

This is a keybinding signal.

The default bindings for this signal are <kbd>Ctrl</kbd>+<kbd>.</kbd> and <kbd>Ctrl</kbd>+<kbd>;</kbd>

move-cursor

def on_move_cursor(self, step: 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 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</kbd> 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>, <kbd>↑</kbd>, <kbd>↓</kbd> move by individual characters/lines
  • <kbd>Ctrl</kbd>+<kbd>←</kbd>, etc. move by words/paragraphs
  • <kbd>Home</kbd> and <kbd>End</kbd> move to the ends of the buffer

paste-clipboard

def on_paste_clipboard(self) -> None: ...

Emitted to paste the contents of the clipboard.

This is a keybinding signal.

The default bindings for this signal are <kbd>Ctrl</kbd>+<kbd>v</kbd> and <kbd>Shift</kbd>+<kbd>Insert</kbd>.

preedit-changed

def on_preedit_changed(self, 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.

toggle-overwrite

def on_toggle_overwrite(self) -> None: ...

Emitted to toggle the overwrite mode.

This is a keybinding signal.

The default bindings for this signal is <kbd>Insert</kbd>.