Skip to content

Gtk.TextView

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

Displays the contents of a TextBuffer.

<picture> <source srcset="multiline-text-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkTextView" src="multiline-text.png"> </picture>

You may wish to begin by reading the conceptual overview, which gives an overview of all the objects and data types related to the text widget and how they work together.

Shortcuts and Gestures

GtkTextView supports the following keyboard shortcuts:

  • <kbd>Shift</kbd>+<kbd>F10</kbd> or <kbd>Menu</kbd> opens the context menu.
  • <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>Clear</kbd> clears the content.

Additionally, the following signals have default keybindings:

Actions

GtkTextView 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.
  • 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 clears the content.

CSS nodes

textview.view
├── border.top
├── border.left
├── text
│   ╰── [selection]
├── border.right
├── border.bottom
╰── [window.popup]

GtkTextView has a main css node with name textview and style class .view, and subnodes for each of the border windows, and the main text area, with names border and text, respectively. The border nodes each get one of the style classes .left, .right, .top or .bottom.

A node representing the selection will appear below the text node.

If a context menu is opened, the window node will appear as a subnode of the main node.

Accessibility

GtkTextView uses the AccessibleRole.text_box role.

Constructors

new

@classmethod
def new(cls) -> Widget

Creates a new GtkTextView.

If you don’t call TextView.set_buffer before using the text view, an empty default buffer will be created for you. Get the buffer with TextView.get_buffer. If you want to specify your own buffer, consider TextView.new_with_buffer.

new_with_buffer

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

Creates a new GtkTextView widget displaying the buffer buffer.

One buffer can be shared among many widgets. buffer may be None to create a default buffer, in which case this function is equivalent to TextView.new. The text view adds its own reference count to the buffer; it does not take over an existing reference.

Parameters:

  • buffer — a GtkTextBuffer

Methods

add_child_at_anchor

def add_child_at_anchor(self, child: Widget, anchor: TextChildAnchor) -> None

Adds a child widget in the text buffer, at the given anchor.

Parameters:

  • child — a GtkWidget
  • anchor — a GtkTextChildAnchor in the GtkTextBuffer for text_view

add_overlay

def add_overlay(self, child: Widget, xpos: int, ypos: int) -> None

Adds child at a fixed coordinate in the GtkTextView's text window.

The xpos and ypos must be in buffer coordinates (see TextView.get_iter_location to convert to buffer coordinates).

child will scroll with the text view.

If instead you want a widget that will not move with the GtkTextView contents see GtkOverlay.

Parameters:

  • child — a GtkWidget
  • xpos — X position of child in window coordinates
  • ypos — Y position of child in window coordinates

backward_display_line

def backward_display_line(self, iter: TextIter) -> bool

Moves the given iter backward by one display (wrapped) line.

A display line is different from a paragraph. Paragraphs are separated by newlines or other paragraph separator characters. Display lines are created by line-wrapping a paragraph. If wrapping is turned off, display lines and paragraphs will be the same. Display lines are divided differently for each view, since they depend on the view’s width; paragraphs are the same in all views, since they depend on the contents of the GtkTextBuffer.

Parameters:

  • iter — a GtkTextIter

backward_display_line_start

def backward_display_line_start(self, iter: TextIter) -> bool

Moves the given iter backward to the next display line start.

A display line is different from a paragraph. Paragraphs are separated by newlines or other paragraph separator characters. Display lines are created by line-wrapping a paragraph. If wrapping is turned off, display lines and paragraphs will be the same. Display lines are divided differently for each view, since they depend on the view’s width; paragraphs are the same in all views, since they depend on the contents of the GtkTextBuffer.

Parameters:

  • iter — a GtkTextIter

buffer_to_window_coords

def buffer_to_window_coords(self, win: TextWindowType | int, buffer_x: int, buffer_y: int) -> tuple[int, int]

Converts buffer coordinates to window coordinates.

Parameters:

  • win — a GtkTextWindowType
  • buffer_x — buffer x coordinate
  • buffer_y — buffer y coordinate

forward_display_line

def forward_display_line(self, iter: TextIter) -> bool

Moves the given iter forward by one display (wrapped) line.

A display line is different from a paragraph. Paragraphs are separated by newlines or other paragraph separator characters. Display lines are created by line-wrapping a paragraph. If wrapping is turned off, display lines and paragraphs will be the same. Display lines are divided differently for each view, since they depend on the view’s width; paragraphs are the same in all views, since they depend on the contents of the GtkTextBuffer.

Parameters:

  • iter — a GtkTextIter

forward_display_line_end

def forward_display_line_end(self, iter: TextIter) -> bool

Moves the given iter forward to the next display line end.

A display line is different from a paragraph. Paragraphs are separated by newlines or other paragraph separator characters. Display lines are created by line-wrapping a paragraph. If wrapping is turned off, display lines and paragraphs will be the same. Display lines are divided differently for each view, since they depend on the view’s width; paragraphs are the same in all views, since they depend on the contents of the GtkTextBuffer.

Parameters:

  • iter — a GtkTextIter

get_accepts_tab

def get_accepts_tab(self) -> bool

Returns whether pressing the <kbd>Tab</kbd> key inserts a tab characters.

See TextView.set_accepts_tab.

get_bottom_margin

def get_bottom_margin(self) -> int

Gets the bottom margin for text in the text_view.

get_buffer

def get_buffer(self) -> TextBuffer

Returns the GtkTextBuffer being displayed by this text view.

The reference count on the buffer is not incremented; the caller of this function won’t own a new reference.

get_cursor_locations

def get_cursor_locations(self, iter: TextIter | None = ...) -> tuple[Gdk.Rectangle, Gdk.Rectangle]

Determine the positions of the strong and weak cursors if the insertion point is at iter.

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 of the paragraph are inserted. The weak cursor location is the location where characters of the directionality opposite to the base direction of the paragraph are inserted.

If iter is None, the actual cursor position is used.

Note that if iter happens to be the actual cursor position, and there is currently an IM preedit sequence being entered, the returned locations will be adjusted to account for the preedit cursor’s offset within the preedit sequence.

The rectangle position is in buffer coordinates; use TextView.buffer_to_window_coords to convert these coordinates to coordinates for one of the windows in the text view.

Parameters:

  • iter — a GtkTextIter

get_cursor_visible

def get_cursor_visible(self) -> bool

Find out whether the cursor should be displayed.

get_editable

def get_editable(self) -> bool

Returns the default editability of the GtkTextView.

Tags in the buffer may override this setting for some ranges of text.

get_extra_menu

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

Gets the menu model that gets added to the context menu or None if none has been set.

get_gutter

def get_gutter(self, win: TextWindowType | int) -> Widget | None

Gets a GtkWidget that has previously been set as gutter.

See TextView.set_gutter.

win must be one of TextWindowType.LEFT, TextWindowType.RIGHT, TextWindowType.TOP, or TextWindowType.BOTTOM.

Parameters:

  • win — a GtkTextWindowType

get_indent

def get_indent(self) -> int

Gets the default indentation of paragraphs in text_view.

Tags in the view’s buffer may override the default. The indentation may be negative.

get_input_hints

def get_input_hints(self) -> InputHints

Gets the input-hints of the GtkTextView.

get_input_purpose

def get_input_purpose(self) -> InputPurpose

Gets the input-purpose of the GtkTextView.

get_iter_at_location

def get_iter_at_location(self, x: int, y: int) -> tuple[bool, TextIter]

Retrieves the iterator at buffer coordinates x and y.

Buffer coordinates are coordinates for the entire buffer, not just the currently-displayed portion. If you have coordinates from an event, you have to convert those to buffer coordinates with TextView.window_to_buffer_coords.

Parameters:

  • x — x position, in buffer coordinates
  • y — y position, in buffer coordinates

get_iter_at_position

def get_iter_at_position(self, x: int, y: int) -> tuple[bool, TextIter, int]

Retrieves the iterator pointing to the character at buffer coordinates x and y.

Buffer coordinates are coordinates for the entire buffer, not just the currently-displayed portion. If you have coordinates from an event, you have to convert those to buffer coordinates with TextView.window_to_buffer_coords.

Note that this is different from TextView.get_iter_at_location, which returns cursor locations, i.e. positions between characters.

Parameters:

  • x — x position, in buffer coordinates
  • y — y position, in buffer coordinates

get_iter_location

def get_iter_location(self, iter: TextIter) -> Gdk.Rectangle

Gets a rectangle which roughly contains the character at iter.

The rectangle position is in buffer coordinates; use TextView.buffer_to_window_coords to convert these coordinates to coordinates for one of the windows in the text view.

Parameters:

  • iter — a GtkTextIter

get_justification

def get_justification(self) -> Justification

Gets the default justification of paragraphs in text_view.

Tags in the buffer may override the default.

get_left_margin

def get_left_margin(self) -> int

Gets the default left margin size of paragraphs in the text_view.

Tags in the buffer may override the default.

get_line_at_y

def get_line_at_y(self, y: int) -> tuple[TextIter, int]

Gets the GtkTextIter at the start of the line containing the coordinate y.

y is in buffer coordinates, convert from window coordinates with TextView.window_to_buffer_coords. If non-None, line_top will be filled with the coordinate of the top edge of the line.

Parameters:

  • y — a y coordinate

get_line_yrange

def get_line_yrange(self, iter: TextIter) -> tuple[int, int]

Gets the y coordinate of the top of the line containing iter, and the height of the line.

The coordinate is a buffer coordinate; convert to window coordinates with TextView.buffer_to_window_coords.

Parameters:

  • iter — a GtkTextIter

get_ltr_context

def get_ltr_context(self) -> Pango.Context

Gets the PangoContext that is used for rendering LTR directed text layouts.

The context may be replaced when CSS changes occur.

get_monospace

def get_monospace(self) -> bool

Gets whether the GtkTextView uses monospace styling.

get_overwrite

def get_overwrite(self) -> bool

Returns whether the GtkTextView is in overwrite mode or not.

get_pixels_above_lines

def get_pixels_above_lines(self) -> int

Gets the default number of pixels to put above paragraphs.

Adding this function with TextView.get_pixels_below_lines is equal to the line space between each paragraph.

get_pixels_below_lines

def get_pixels_below_lines(self) -> int

Gets the default number of pixels to put below paragraphs.

The line space is the sum of the value returned by this function and the value returned by TextView.get_pixels_above_lines.

get_pixels_inside_wrap

def get_pixels_inside_wrap(self) -> int

Gets the default number of pixels to put between wrapped lines inside a paragraph.

get_right_margin

def get_right_margin(self) -> int

Gets the default right margin for text in text_view.

Tags in the buffer may override the default.

get_rtl_context

def get_rtl_context(self) -> Pango.Context

Gets the PangoContext that is used for rendering RTL directed text layouts.

The context may be replaced when CSS changes occur.

get_tabs

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

Gets the default tabs for text_view.

Tags in the buffer may override the defaults. The returned array will be None if “standard” (8-space) tabs are used. Free the return value with Pango.TabArray.free.

get_top_margin

def get_top_margin(self) -> int

Gets the top margin for text in the text_view.

get_visible_offset

def get_visible_offset(self) -> tuple[float, float]

Gets the X,Y offset in buffer coordinates of the top-left corner of the textview's text contents.

This allows for more-precise positioning than what is provided by TextView.get_visible_rect as you can discover what device pixel is being quantized for text positioning.

You might want this when making ulterior widgets align with quantized device pixels of the textview contents such as line numbers.

get_visible_rect

def get_visible_rect(self) -> Gdk.Rectangle

Fills visible_rect with the currently-visible region of the buffer, in buffer coordinates.

Convert to window coordinates with TextView.buffer_to_window_coords.

get_wrap_mode

def get_wrap_mode(self) -> WrapMode

Gets the line wrapping for the view.

im_context_filter_keypress

def im_context_filter_keypress(self, event: Gdk.Event) -> bool

Allow the GtkTextView input method to internally handle key press and release events.

If this function returns True, then no further processing should be done for this key event. See IMContext.filter_keypress.

Note that you are expected to call this function from your handler when overriding key event handling. This is needed in the case when you need to insert your own key handling between the input method and the default key event handling of the GtkTextView.

static gboolean
gtk_foo_bar_key_press_event (GtkWidget *widget,
                             GdkEvent  *event)
{
  guint keyval;

  gdk_event_get_keyval ((GdkEvent*)event, &keyval);

  if (keyval == GDK_KEY_Return || keyval == GDK_KEY_KP_Enter)
    {
      if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (widget), event))
        return TRUE;
    }

  // Do some stuff

  return GTK_WIDGET_CLASS (gtk_foo_bar_parent_class)->key_press_event (widget, event);
}

Parameters:

  • event — the key event

move_mark_onscreen

def move_mark_onscreen(self, mark: TextMark) -> bool

Moves a mark within the buffer so that it's located within the currently-visible text area.

Parameters:

  • mark — a GtkTextMark

move_overlay

def move_overlay(self, child: Widget, xpos: int, ypos: int) -> None

Updates the position of a child.

See TextView.add_overlay.

Parameters:

  • child — a widget already added with TextView.add_overlay
  • xpos — new X position in buffer coordinates
  • ypos — new Y position in buffer coordinates

move_visually

def move_visually(self, iter: TextIter, count: int) -> bool

Move the iterator a given number of characters visually, treating it as the strong cursor position.

If count is positive, then the new strong cursor position will be count positions to the right of the old cursor position. If count is negative then the new strong cursor position will be count positions to the left of the old cursor position.

In the presence of bi-directional text, the correspondence between logical and visual order will depend on the direction of the current run, and there may be jumps when the cursor is moved off of the end of a run.

Parameters:

  • iter — a GtkTextIter
  • count — number of characters to move (negative moves left, positive moves right)

place_cursor_onscreen

def place_cursor_onscreen(self) -> bool

Moves the cursor to the currently visible region of the buffer.

remove

def remove(self, child: Widget) -> None

Removes a child widget from text_view.

Parameters:

  • child — the child to remove
def reset_cursor_blink(self) -> None

Ensures that the cursor is shown.

This also resets the time that it will stay blinking (or visible, in case blinking is disabled).

This function should be called in response to user input (e.g. from derived classes that override the textview's event handlers).

reset_im_context

def reset_im_context(self) -> None

Reset the input method context of the text view if needed.

This can be necessary in the case where modifying the buffer would confuse on-going input method behavior.

scroll_mark_onscreen

def scroll_mark_onscreen(self, mark: TextMark) -> None

Scrolls text_view the minimum distance such that mark is contained within the visible area of the widget.

Parameters:

  • mark — a mark in the buffer for text_view

scroll_to_iter

def scroll_to_iter(self, iter: TextIter, within_margin: float, use_align: bool, xalign: float, yalign: float) -> bool

Scrolls text_view so that iter is on the screen in the position indicated by xalign and yalign.

An alignment of 0.0 indicates left or top, 1.0 indicates right or bottom, 0.5 means center. If use_align is False, the text scrolls the minimal distance to get the mark onscreen, possibly not scrolling at all. The effective screen for purposes of this function is reduced by a margin of size within_margin.

Note that this function uses the currently-computed height of the lines in the text buffer. Line heights are computed in an idle handler; so this function may not have the desired effect if it’s called before the height computations. To avoid oddness, consider using TextView.scroll_to_mark which saves a point to be scrolled to after line validation.

Parameters:

  • iter — a GtkTextIter
  • within_margin — margin as a [0.0,0.5) fraction of screen size
  • use_align — whether to use alignment arguments (if False, just get the mark onscreen)
  • xalign — horizontal alignment of mark within visible area
  • yalign — vertical alignment of mark within visible area

scroll_to_mark

def scroll_to_mark(self, mark: TextMark, within_margin: float, use_align: bool, xalign: float, yalign: float) -> None

Scrolls text_view so that mark is on the screen in the position indicated by xalign and yalign.

An alignment of 0.0 indicates left or top, 1.0 indicates right or bottom, 0.5 means center. If use_align is False, the text scrolls the minimal distance to get the mark onscreen, possibly not scrolling at all. The effective screen for purposes of this function is reduced by a margin of size within_margin.

Parameters:

  • mark — a GtkTextMark
  • within_margin — margin as a [0.0,0.5) fraction of screen size
  • use_align — whether to use alignment arguments (if False, just get the mark onscreen)
  • xalign — horizontal alignment of mark within visible area
  • yalign — vertical alignment of mark within visible area

set_accepts_tab

def set_accepts_tab(self, accepts_tab: bool) -> None

Sets the behavior of the text widget when the <kbd>Tab</kbd> key is pressed.

If accepts_tab is True, a tab character is inserted. If accepts_tab is False the keyboard focus is moved to the next widget in the focus chain.

Focus can always be moved using <kbd>Ctrl</kbd>+<kbd>Tab</kbd>.

Parameters:

  • accepts_tabTrue if pressing the Tab key should insert a tab character, False, if pressing the Tab key should move the keyboard focus.

set_bottom_margin

def set_bottom_margin(self, bottom_margin: int) -> None

Sets the bottom margin for text in text_view.

Note that this function is confusingly named. In CSS terms, the value set here is padding.

Parameters:

  • bottom_margin — bottom margin in pixels

set_buffer

def set_buffer(self, buffer: TextBuffer | None = ...) -> None

Sets buffer as the buffer being displayed by text_view.

The previous buffer displayed by the text view is unreferenced, and a reference is added to buffer. If you owned a reference to buffer before passing it to this function, you must remove that reference yourself; GtkTextView will not “adopt” it.

Parameters:

  • buffer — a GtkTextBuffer

set_cursor_visible

def set_cursor_visible(self, setting: bool) -> None

Toggles whether the insertion point should be displayed.

A buffer with no editable text probably shouldn’t have a visible cursor, so you may want to turn the cursor off.

Note that this property may be overridden by the Settings.gtk-keynav-use-caret setting.

Parameters:

  • setting — whether to show the insertion cursor

set_editable

def set_editable(self, setting: bool) -> None

Sets the default editability of the GtkTextView.

You can override this default setting with tags in the buffer, using the “editable” attribute of tags.

Parameters:

  • setting — whether it’s editable

set_extra_menu

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

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

You can pass None to remove a previously set extra menu.

Parameters:

  • model — a GMenuModel

set_gutter

def set_gutter(self, win: TextWindowType | int, widget: Widget | None = ...) -> None

Places widget into the gutter specified by win.

win must be one of TextWindowType.LEFT, TextWindowType.RIGHT, TextWindowType.TOP, or TextWindowType.BOTTOM.

Parameters:

  • win — a GtkTextWindowType
  • widget — a GtkWidget

set_indent

def set_indent(self, indent: int) -> None

Sets the default indentation for paragraphs in text_view.

Tags in the buffer may override the default.

Parameters:

  • indent — indentation in pixels

set_input_hints

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

Sets the input-hints of the GtkTextView.

The input-hints allow input methods to fine-tune their behaviour.

Parameters:

  • hints — the hints

set_input_purpose

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

Sets the input-purpose of the GtkTextView.

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

Parameters:

  • purpose — the purpose

set_justification

def set_justification(self, justification: Justification | int) -> None

Sets the default justification of text in text_view.

Tags in the view’s buffer may override the default.

Parameters:

  • justification — justification

set_left_margin

def set_left_margin(self, left_margin: int) -> None

Sets the default left margin for text in text_view.

Tags in the buffer may override the default.

Note that this function is confusingly named. In CSS terms, the value set here is padding.

Parameters:

  • left_margin — left margin in pixels

set_monospace

def set_monospace(self, monospace: bool) -> None

Sets whether the GtkTextView should display text in monospace styling.

Parameters:

  • monospaceTrue to request monospace styling

set_overwrite

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

Changes the GtkTextView overwrite mode.

Parameters:

  • overwriteTrue to turn on overwrite mode, False to turn it off

set_pixels_above_lines

def set_pixels_above_lines(self, pixels_above_lines: int) -> None

Sets the default number of blank pixels above paragraphs in text_view.

Tags in the buffer for text_view may override the defaults.

Parameters:

  • pixels_above_lines — pixels above paragraphs

set_pixels_below_lines

def set_pixels_below_lines(self, pixels_below_lines: int) -> None

Sets the default number of pixels of blank space to put below paragraphs in text_view.

May be overridden by tags applied to text_view’s buffer.

Parameters:

  • pixels_below_lines — pixels below paragraphs

set_pixels_inside_wrap

def set_pixels_inside_wrap(self, pixels_inside_wrap: int) -> None

Sets the default number of pixels of blank space to leave between display/wrapped lines within a paragraph.

May be overridden by tags in text_view’s buffer.

Parameters:

  • pixels_inside_wrap — default number of pixels between wrapped lines

set_right_margin

def set_right_margin(self, right_margin: int) -> None

Sets the default right margin for text in the text view.

Tags in the buffer may override the default.

Note that this function is confusingly named. In CSS terms, the value set here is padding.

Parameters:

  • right_margin — right margin in pixels

set_tabs

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

Sets the default tab stops for paragraphs in text_view.

Tags in the buffer may override the default.

Parameters:

  • tabs — tabs as a PangoTabArray

set_top_margin

def set_top_margin(self, top_margin: int) -> None

Sets the top margin for text in text_view.

Note that this function is confusingly named. In CSS terms, the value set here is padding.

Parameters:

  • top_margin — top margin in pixels

set_wrap_mode

def set_wrap_mode(self, wrap_mode: WrapMode | int) -> None

Sets the line wrapping for the view.

Parameters:

  • wrap_mode — a GtkWrapMode

starts_display_line

def starts_display_line(self, iter: TextIter) -> bool

Determines whether iter is at the start of a display line.

See TextView.forward_display_line for an explanation of display lines vs. paragraphs.

Parameters:

  • iter — a GtkTextIter

window_to_buffer_coords

def window_to_buffer_coords(self, win: TextWindowType | int, window_x: int, window_y: int) -> tuple[int, int]

Converts coordinates on the window identified by win to buffer coordinates.

Parameters:

  • win — a GtkTextWindowType
  • window_x — window x coordinate
  • window_y — window y coordinate

Virtual methods

do_backspace

def do_backspace(self) -> None

The class handler for the GtkTextView::backspace keybinding signal.

do_copy_clipboard

def do_copy_clipboard(self) -> None

The class handler for the GtkTextView::copy-clipboard keybinding signal.

do_cut_clipboard

def do_cut_clipboard(self) -> None

The class handler for the GtkTextView::cut-clipboard keybinding signal

do_delete_from_cursor

def do_delete_from_cursor(self, type: DeleteType | int, count: int) -> None

The class handler for the GtkTextView::delete-from-cursor keybinding signal.

do_extend_selection

def do_extend_selection(self, granularity: TextExtendSelection | int, location: TextIter, start: TextIter, end: TextIter) -> bool

The class handler for the GtkTextView::extend-selection signal.

do_insert_at_cursor

def do_insert_at_cursor(self, str: str) -> None

The class handler for the GtkTextView::insert-at-cursor keybinding signal.

do_insert_emoji

def do_insert_emoji(self) -> None

The class handler for the GtkTextView::insert-emoji signal.

do_move_cursor

def do_move_cursor(self, step: MovementStep | int, count: int, extend_selection: bool) -> None

The class handler for the GtkTextView::move-cursor keybinding signal.

do_paste_clipboard

def do_paste_clipboard(self) -> None

The class handler for the GtkTextView::paste-clipboard keybinding signal.

do_set_anchor

def do_set_anchor(self) -> None

The class handler for the GtkTextView::set-anchor keybinding signal.

do_snapshot_layer

def do_snapshot_layer(self, layer: TextViewLayer | int, snapshot: Snapshot) -> None

The snapshot_layer vfunc is called before and after the text view is drawing its own text. Applications can override this vfunc in a subclass to draw customized content underneath or above the text. In the TextViewLayer.BELOW_TEXT and TextViewLayer.ABOVE_TEXT layers the drawing is done in the buffer coordinate space.

do_toggle_overwrite

def do_toggle_overwrite(self) -> None

The class handler for the GtkTextView::toggle-overwrite keybinding signal.

Properties

accepts_tab

accepts_tab: bool  # read/write

Whether Tab will result in a tab character being entered.

bottom_margin

bottom_margin: int  # read/write

The bottom margin for text in the text view.

Note that this property is confusingly named. In CSS terms, the value set here is padding, and it is applied in addition to the padding from the theme.

Don't confuse this property with Widget.margin-bottom.

buffer

buffer: TextBuffer  # read/write

The buffer which is displayed.

cursor_visible

cursor_visible: bool  # read/write

If the insertion cursor is shown.

editable

editable: bool  # read/write

Whether the text can be modified by the user.

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 IM (input method) module should be used for this text_view.

See IMMulticontext.

Setting this to a non-None value overrides the system-wide IM module setting. See the GtkSettings Settings.gtk-im-module property.

indent

indent: int  # read/write

Amount to indent the paragraph, in pixels.

A negative value of indent will produce a hanging indentation. That is, the first line will have the full width, and subsequent lines will be indented by the absolute value of indent.

input_hints

input_hints: InputHints | int  # read/write

Additional hints (beyond TextView.input-purpose) that allow input methods to fine-tune their behaviour.

input_purpose

input_purpose: InputPurpose | int  # read/write

The purpose of this text field.

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

justification

justification: Justification | int  # read/write

Left, right, or center justification.

left_margin

left_margin: int  # read/write

The default left margin for text in the text view.

Tags in the buffer may override the default.

Note that this property is confusingly named. In CSS terms, the value set here is padding, and it is applied in addition to the padding from the theme.

monospace

monospace: bool  # read/write

Whether text should be displayed in a monospace font.

If True, set the .monospace style class on the text view to indicate that a monospace font is desired.

overwrite

overwrite: bool  # read/write

Whether entered text overwrites existing contents.

pixels_above_lines

pixels_above_lines: int  # read/write

Pixels of blank space above paragraphs.

pixels_below_lines

pixels_below_lines: int  # read/write

Pixels of blank space below paragraphs.

pixels_inside_wrap

pixels_inside_wrap: int  # read/write

Pixels of blank space between wrapped lines in a paragraph.

right_margin

right_margin: int  # read/write

The default right margin for text in the text view.

Tags in the buffer may override the default.

Note that this property is confusingly named. In CSS terms, the value set here is padding, and it is applied in addition to the padding from the theme.

tabs

tabs: Pango.TabArray  # read/write

Custom tabs for this text.

top_margin

top_margin: int  # read/write

The top margin for text in the text view.

Note that this property is confusingly named. In CSS terms, the value set here is padding, and it is applied in addition to the padding from the theme.

Don't confuse this property with Widget.margin-top.

wrap_mode

wrap_mode: WrapMode | int  # read/write

Whether to wrap lines never, at word boundaries, or at character boundaries.

Signals

backspace

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

Gets emitted when the user asks for it.

The ::backspace signal 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: ...

Gets emitted to copy the selection to the clipboard.

The ::copy-clipboard signal 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: ...

Gets emitted to cut the selection to the clipboard.

The ::cut-clipboard signal 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: ...

Gets emitted when the user initiates a text deletion.

The ::delete-from-cursor signal 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, <kbd>Ctrl</kbd>+<kbd>Delete</kbd> for deleting a word and <kbd>Ctrl</kbd>+<kbd>Backspace</kbd> for deleting a word backwards.

extend-selection

def on_extend_selection(self, granularity: TextExtendSelection, location: TextIter, start: TextIter, end: TextIter) -> bool: ...

Emitted when the selection needs to be extended at location.

insert-at-cursor

def on_insert_at_cursor(self, string: str) -> None: ...

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

The ::insert-at-cursor signal is a keybinding signal.

This signal has no default bindings.

insert-emoji

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

Gets emitted to present the Emoji chooser for the text_view.

The ::insert-emoji signal 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_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 text_view, this signal causes the viewport to be moved instead.

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
  • <kbd>PgUp</kbd> and <kbd>PgDn</kbd> move vertically by pages
  • <kbd>Ctrl</kbd>+<kbd>PgUp</kbd> and <kbd>Ctrl</kbd>+<kbd>PgDn</kbd> move horizontally by pages

move-viewport

def on_move_viewport(self, step: ScrollStep, count: int) -> None: ...

Gets emitted to move the viewport.

The ::move-viewport signal is a keybinding signal, which can be bound to key combinations to allow the user to move the viewport, i.e. change what part of the text view is visible in a containing scrolled window.

There are no default bindings for this signal.

paste-clipboard

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

Gets emitted to paste the contents of the clipboard into the text view.

The ::paste-clipboard signal 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 preedit text of the active IM 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.

This signal is only emitted if the text at the given position is actually editable.

select-all

def on_select_all(self, select: bool) -> None: ...

Gets emitted to select or unselect the complete contents of the text view.

The ::select-all signal is a keybinding signal.

The default bindings for this signal are <kbd>Ctrl</kbd>+<kbd>a</kbd> and <kbd>Ctrl</kbd>+<kbd>/</kbd> for selecting and <kbd>Shift</kbd>+<kbd>Ctrl</kbd>+<kbd>a</kbd> and <kbd>Ctrl</kbd>+<kbd>\</kbd> for unselecting.

set-anchor

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

Gets emitted when the user initiates settings the "anchor" mark.

The ::set-anchor signal is a keybinding signal which gets emitted when the user initiates setting the "anchor" mark. The "anchor" mark gets placed at the same position as the "insert" mark.

This signal has no default bindings.

toggle-cursor-visible

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

Gets emitted to toggle the cursor-visible property.

The ::toggle-cursor-visible signal is a keybinding signal.

The default binding for this signal is <kbd>F7</kbd>.

toggle-overwrite

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

Gets emitted to toggle the overwrite mode of the text view.

The ::toggle-overwrite signal is a keybinding signal.

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