Gtk.Text

g GObject.GInterface GObject.GInterface Gtk.Accessible Gtk.Accessible GObject.GInterface->Gtk.Accessible Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable Gtk.ConstraintTarget Gtk.ConstraintTarget GObject.GInterface->Gtk.ConstraintTarget Gtk.Editable Gtk.Editable GObject.GInterface->Gtk.Editable GObject.InitiallyUnowned GObject.InitiallyUnowned Gtk.Widget Gtk.Widget GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Accessible->Gtk.Widget Gtk.Buildable->Gtk.Widget Gtk.ConstraintTarget->Gtk.Widget Gtk.Text Gtk.Text Gtk.Editable->Gtk.Text Gtk.Widget->Gtk.Text

Subclasses:

None

Methods

Inherited:

Gtk.Widget (181), GObject.Object (37), Gtk.Accessible (15), Gtk.Buildable (1), Gtk.Editable (27)

Structs:

Gtk.WidgetClass (18), GObject.ObjectClass (5)

class

new ()

class

new_with_buffer (buffer)

compute_cursor_extents (position)

get_activates_default ()

get_attributes ()

get_buffer ()

get_enable_emoji_completion ()

get_extra_menu ()

get_input_hints ()

get_input_purpose ()

get_invisible_char ()

get_max_length ()

get_overwrite_mode ()

get_placeholder_text ()

get_propagate_text_width ()

get_tabs ()

get_text_length ()

get_truncate_multiline ()

get_visibility ()

grab_focus_without_selecting ()

set_activates_default (activates)

set_attributes (attrs)

set_buffer (buffer)

set_enable_emoji_completion (enable_emoji_completion)

set_extra_menu (model)

set_input_hints (hints)

set_input_purpose (purpose)

set_invisible_char (ch)

set_max_length (length)

set_overwrite_mode (overwrite)

set_placeholder_text (text)

set_propagate_text_width (propagate_text_width)

set_tabs (tabs)

set_truncate_multiline (truncate_multiline)

set_visibility (visible)

unset_invisible_char ()

Virtual Methods

Inherited:

Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9), Gtk.Editable (9)

Properties

Inherited:

Gtk.Widget (34), Gtk.Accessible (1), Gtk.Editable (8)

Name

Type

Flags

Short Description

activates-default

bool

r/w/en

attributes

Pango.AttrList

r/w/en

buffer

Gtk.EntryBuffer

r/w/c/en

enable-emoji-completion

bool

r/w/en

extra-menu

Gio.MenuModel

r/w/en

im-module

str

r/w/en

input-hints

Gtk.InputHints

r/w/en

input-purpose

Gtk.InputPurpose

r/w/en

invisible-char

int

r/w/en

invisible-char-set

bool

r/w

max-length

int

r/w/en

overwrite-mode

bool

r/w/en

placeholder-text

str

r/w/en

propagate-text-width

bool

r/w/en

scroll-offset

int

r/en

tabs

Pango.TabArray

r/w/en

truncate-multiline

bool

r/w/en

visibility

bool

r/w/en

Signals

Inherited:

Gtk.Widget (13), GObject.Object (1), Gtk.Editable (3)

Name

Short Description

activate

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

backspace

Emitted when the user asks for it.

copy-clipboard

Emitted to copy the selection to the clipboard.

cut-clipboard

Emitted to cut the selection to the clipboard.

delete-from-cursor

Emitted when the user initiates a text deletion.

insert-at-cursor

Emitted when the user initiates the insertion of a fixed string at the cursor.

insert-emoji

Emitted to present the Emoji chooser for the widget.

move-cursor

Emitted when the user initiates a cursor movement.

paste-clipboard

Emitted to paste the contents of the clipboard.

preedit-changed

Emitted when the preedit text changes.

toggle-overwrite

Emitted to toggle the overwrite mode of the GtkText.

Fields

Inherited:

Gtk.Widget (13), GObject.Object (1), Gtk.Editable (3)

Name

Type

Access

Description

parent_instance

Gtk.Widget

r

Class Details

class Gtk.Text(**kwargs)
Bases:

Gtk.Widget, Gtk.Editable

Abstract:

No

The GtkText widget is a single-line text entry widget.

GtkText is the common implementation of single-line text editing that is shared between [class`Gtk`.Entry], [class`Gtk`.PasswordEntry], [class`Gtk`.SpinButton], and other widgets. In all of these, GtkText is used as the delegate for the [iface`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 [method`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 [method`Gtk`.Text.set_invisible_char].

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

If you need multi-line editable text, look at [class`Gtk`.TextView].

CSS nodes

`` 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 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.

classmethod new()[source]
Returns:

a new GtkText.

Return type:

Gtk.Widget

Creates a new GtkText.

classmethod new_with_buffer(buffer)[source]
Parameters:

buffer (Gtk.EntryBuffer) – The buffer to use for the new GtkText.

Returns:

a new GtkText

Return type:

Gtk.Widget

Creates a new GtkText with the specified text buffer.

compute_cursor_extents(position)[source]
Parameters:

position (int) – the character position

Returns:

strong:

location to store the strong cursor position

weak:

location to store the weak cursor position

Return type:

(strong: Graphene.Rect, weak: 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.

New in version 4.4.

get_activates_default()[source]
Returns:

True if the GtkText will activate the default widget

Return type:

bool

Returns whether pressing Enter will activate the default widget for the window containing self.

See [method`Gtk`.Text.set_activates_default].

get_attributes()[source]
Returns:

the attribute list

Return type:

Pango.AttrList or None

Gets the attribute list that was set on the GtkText.

See [method`Gtk`.Text.set_attributes].

get_buffer()[source]
Returns:

A GtkEntryBuffer object.

Return type:

Gtk.EntryBuffer

Get the GtkEntryBuffer object which holds the text for this widget.

get_enable_emoji_completion()[source]
Returns:

True if Emoji completion is enabled

Return type:

bool

Returns whether Emoji completion is enabled for this GtkText widget.

get_extra_menu()[source]
Returns:

the menu model

Return type:

Gio.MenuModel or None

Gets the menu model for extra items in the context menu.

See [method`Gtk`.Text.set_extra_menu].

get_input_hints()[source]
Return type:

Gtk.InputHints

Gets the input hints of the GtkText.

get_input_purpose()[source]
Return type:

Gtk.InputPurpose

Gets the input purpose of the GtkText.

get_invisible_char()[source]
Returns:

the current invisible str, or 0, if text does not show invisible text at all.

Return type:

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 [method`Gtk`.Text.set_invisible_char].

get_max_length()[source]
Returns:

the maximum allowed number of characters in GtkText, or 0 if there is no maximum.

Return type:

int

Retrieves the maximum allowed length of the text in self.

See [method`Gtk`.Text.set_max_length].

This is equivalent to getting self's GtkEntryBuffer and calling [method`Gtk`.EntryBuffer.get_max_length] on it.

get_overwrite_mode()[source]
Returns:

whether the text is overwritten when typing

Return type:

bool

Gets whether text is overwritten when typing in the GtkText.

See [method`Gtk`.Text.set_overwrite_mode].

get_placeholder_text()[source]
Returns:

the placeholder text

Return type:

str or None

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

If no placeholder text has been set, None will be returned.

get_propagate_text_width()[source]
Returns:

True if self will propagate the text width

Return type:

bool

Returns whether the GtkText will grow and shrink with the content.

get_tabs()[source]
Returns:

the tabstops

Return type:

Pango.TabArray or None

Gets the tabstops that were set on the GtkText.

See [method`Gtk`.Text.set_tabs].

get_text_length()[source]
Returns:

the current number of characters in GtkText, or 0 if there are none.

Return type:

int

Retrieves the current length of the text in self.

This is equivalent to getting self's GtkEntryBuffer and calling [method`Gtk`.EntryBuffer.get_length] on it.

get_truncate_multiline()[source]
Returns:

True if self will truncate multi-line text

Return type:

bool

Returns whether the GtkText will truncate multi-line text that is pasted into the widget

get_visibility()[source]
Returns:

True if the text is currently visible

Return type:

bool

Retrieves whether the text in self is visible.

grab_focus_without_selecting()[source]
Returns:

True if focus is now inside self

Return type:

bool

Causes self to have keyboard focus.

It behaves like [method`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.

set_activates_default(activates)[source]
Parameters:

activates (bool) – True to activate window’s default widget on Enter keypress

If activates is 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.

set_attributes(attrs)[source]
Parameters:

attrs (Pango.AttrList or None) – a PangoAttrList

Sets attributes that are applied to the text.

set_buffer(buffer)[source]
Parameters:

buffer (Gtk.EntryBuffer) – a GtkEntryBuffer

Set the GtkEntryBuffer object which holds the text for this widget.

set_enable_emoji_completion(enable_emoji_completion)[source]
Parameters:

enable_emoji_completion (bool) – True to enable Emoji completion

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.

set_extra_menu(model)[source]
Parameters:

model (Gio.MenuModel or None) – a GMenuModel

Sets a menu model to add when constructing the context menu for self.

set_input_hints(hints)[source]
Parameters:

hints (Gtk.InputHints) – the hints

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

set_input_purpose(purpose)[source]
Parameters:

purpose (Gtk.InputPurpose) – the purpose

Sets the input purpose of the GtkText.

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

set_invisible_char(ch)[source]
Parameters:

ch (str) – a Unicode character

Sets the character to use when in “password mode”.

By default, GTK picks the best invisible str available in the current font. If you set the invisible str to 0, then the user will get no feedback at all; there will be no text on the screen as they type.

set_max_length(length)[source]
Parameters:

length (int) – 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.

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 [method`Gtk`.EntryBuffer.set_max_length] on it.

set_overwrite_mode(overwrite)[source]
Parameters:

overwrite (bool) – new value

Sets whether the text is overwritten when typing in the GtkText.

set_placeholder_text(text)[source]
Parameters:

text (str or None) – a string to be displayed when self is empty and unfocused

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.

set_propagate_text_width(propagate_text_width)[source]
Parameters:

propagate_text_width (bool) – True to propagate the text width

Sets whether the GtkText should grow and shrink with the content.

set_tabs(tabs)[source]
Parameters:

tabs (Pango.TabArray or None) – a PangoTabArray

Sets tabstops that are applied to the text.

set_truncate_multiline(truncate_multiline)[source]
Parameters:

truncate_multiline (bool) – True to truncate multi-line text

Sets whether the GtkText should truncate multi-line text that is pasted into the widget.

set_visibility(visible)[source]
Parameters:

visible (bool) – True if the contents of the GtkText are displayed as plaintext

Sets whether the contents of the GtkText are visible or not.

When visibility is set to False, characters are displayed as the invisible str, 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 [method`Gtk`.Text.set_invisible_char].

Note that you probably want to set [property`Gtk`.Text:input-purpose] to Gtk.InputPurpose.PASSWORD or Gtk.InputPurpose.PIN to inform input methods about the purpose of this self, in addition to setting visibility to False.

unset_invisible_char()[source]

Unsets the invisible str.

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

Signal Details

Gtk.Text.signals.activate(text)
Signal Name:

activate

Flags:

RUN_LAST, ACTION

Parameters:

text (Gtk.Text) – The object which received the signal

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.

Gtk.Text.signals.backspace(text)
Signal Name:

backspace

Flags:

RUN_LAST, ACTION

Parameters:

text (Gtk.Text) – The object which received the signal

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>.

Gtk.Text.signals.copy_clipboard(text)
Signal Name:

copy-clipboard

Flags:

RUN_LAST, ACTION

Parameters:

text (Gtk.Text) – The object which received the signal

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>.

Gtk.Text.signals.cut_clipboard(text)
Signal Name:

cut-clipboard

Flags:

RUN_LAST, ACTION

Parameters:

text (Gtk.Text) – The object which received the signal

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>.

Gtk.Text.signals.delete_from_cursor(text, type, count)
Signal Name:

delete-from-cursor

Flags:

RUN_LAST, ACTION

Parameters:
  • text (Gtk.Text) – The object which received the signal

  • type (Gtk.DeleteType) – the granularity of the deletion, as a GtkDeleteType

  • count (int) – the number of type units to delete

Emitted when the user initiates a text deletion.

This is a keybinding signal.

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

Gtk.Text.signals.insert_at_cursor(text, string)
Signal Name:

insert-at-cursor

Flags:

RUN_LAST, ACTION

Parameters:
  • text (Gtk.Text) – The object which received the signal

  • string (str) – the string to insert

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.

Gtk.Text.signals.insert_emoji(text)
Signal Name:

insert-emoji

Flags:

RUN_LAST, ACTION

Parameters:

text (Gtk.Text) – The object which received the signal

Emitted to present the Emoji chooser for the widget.

This is a keybinding signal.

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

Gtk.Text.signals.move_cursor(text, step, count, extend)
Signal Name:

move-cursor

Flags:

RUN_LAST, ACTION

Parameters:
  • text (Gtk.Text) – The object which received the signal

  • step (Gtk.MovementStep) – the granularity of the move, as a GtkMovementStep

  • count (int) – the number of step units to move

  • extend (bool) – True if the move should extend the selection

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 g_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

Gtk.Text.signals.paste_clipboard(text)
Signal Name:

paste-clipboard

Flags:

RUN_LAST, ACTION

Parameters:

text (Gtk.Text) – The object which received the signal

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>.

Gtk.Text.signals.preedit_changed(text, preedit)
Signal Name:

preedit-changed

Flags:

RUN_LAST, ACTION

Parameters:
  • text (Gtk.Text) – The object which received the signal

  • preedit (str) – the current preedit string

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.

Gtk.Text.signals.toggle_overwrite(text)
Signal Name:

toggle-overwrite

Flags:

RUN_LAST, ACTION

Parameters:

text (Gtk.Text) – The object which received the signal

Emitted to toggle the overwrite mode of the GtkText.

This is a keybinding signal.

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

Property Details

Gtk.Text.props.activates_default
Name:

activates-default

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether to activate the default widget when Enter is pressed.

Gtk.Text.props.attributes
Name:

attributes

Type:

Pango.AttrList

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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.

Gtk.Text.props.buffer
Name:

buffer

Type:

Gtk.EntryBuffer

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT, EXPLICIT_NOTIFY

The GtkEntryBuffer object which stores the text.

Gtk.Text.props.enable_emoji_completion
Name:

enable-emoji-completion

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether to suggest Emoji replacements.

Gtk.Text.props.extra_menu
Name:

extra-menu

Type:

Gio.MenuModel

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.Text.props.im_module
Name:

im-module

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Which IM (input method) module should be used for this self.

See [class`Gtk`.IMMulticontext].

Setting this to a non-None value overrides the system-wide IM module setting. See the [property`Gtk`.Settings:gtk-im-module] property.

Gtk.Text.props.input_hints
Name:

input-hints

Type:

Gtk.InputHints

Default Value:

Gtk.InputHints.NONE

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.Text.props.input_purpose
Name:

input-purpose

Type:

Gtk.InputPurpose

Default Value:

Gtk.InputPurpose.FREE_FORM

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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 Gtk.InputPurpose.PASSWORD or Gtk.InputPurpose.PIN is independent from setting [property`Gtk`.Text:visibility].

Gtk.Text.props.invisible_char
Name:

invisible-char

Type:

int

Default Value:

42

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.Text.props.invisible_char_set
Name:

invisible-char-set

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE

Whether the invisible str has been set for the GtkText.

Gtk.Text.props.max_length
Name:

max-length

Type:

int

Default Value:

0

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Maximum number of characters that are allowed.

Zero indicates no limit.

Gtk.Text.props.overwrite_mode
Name:

overwrite-mode

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

If text is overwritten when typing in the GtkText.

Gtk.Text.props.placeholder_text
Name:

placeholder-text

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.Text.props.propagate_text_width
Name:

propagate-text-width

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether the widget should grow and shrink with the content.

Gtk.Text.props.scroll_offset
Name:

scroll-offset

Type:

int

Default Value:

0

Flags:

READABLE, EXPLICIT_NOTIFY

Number of pixels scrolled of the screen to the left.

Gtk.Text.props.tabs
Name:

tabs

Type:

Pango.TabArray

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

A list of tabstops to apply to the text of the GtkText.

Gtk.Text.props.truncate_multiline
Name:

truncate-multiline

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.Text.props.visibility
Name:

visibility

Type:

bool

Default Value:

True

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

If False, the text is masked with the “invisible str”.