TextView

A TextView widget is able to display large amounts of text. It can be used to provide both editing capabilities and viewing-only functionality.

Constructor

The TextView can be constructed using the following:

textview = Gtk.TextView(textbuffer)

The textbuffer argument should be set to a TextBuffer object.

Methods

A TextBuffer can be set on the TextView with:

textview.set_buffer(textbuffer)

When constructing a TextView, a TextBuffer is automatically created however not directly usable. To name the TextBuffer and gain access call:

textbuffer = textview.get_buffer()

Setting the justification type of the text within the TextView can be configured with the method:

textview.set_justification(justification)

The justification value can be set to one of the following constants; Gtk.Justification.LEFT, Gtk.Justification.RIGHT, Gtk.Justification.CENTER, or Gtk.Justification.FILL.

The TextView supports a variety of wrap modes which can be configured with:

textview.set_wrap_mode(wrap_mode)

The wrap_mode value can be set to one of the following constants; Gtk.WrapMode.NONE, Gtk.WrapMode.CHAR, Gtk.WrapMode.WORD or Gtk.WrapMode.WORDCHAR.

Whether the TextView can be edited or not can be configured using:

textview.set_editable(editable)

When editable is set to False, the user will not be able to add or delete text within the TextView.

Another useful function is to be able to remove the cursor from view:

textview.set_cursor_visible(visible)

When using .set_editable(), it is recommended to also use .set_cursor_visible() and set it to False for usability reasons.

To enable overwrite mode when adding new characters to the TextView, enable with:

textview.set_overwrite(overwrite)

If overwrite is set to True, existing characters will be overwritten with new characters when typed.

Margins can be set within the TextView with:

textview.set_left_margin(margin)
textview.set_right_margin(margin)
textview.set_top_margin(margin)
textview.set_bottom_margin(margin)

The margin value must be set to an integer value which determines the number of pixels of space within the margin.

Indents within the TextView are defined with the methods:

textview.set_indent(indent)

The indent value should be an integer which indicates how the indent appears for new paragraphs of text.

Spaicing between lines can be modified by specifying the number of pixels:

textview.set_pixels_above_lines(pixels)
textview.set_pixels_below_lines(pixels)

To request that the TextView use monospaced font in the view, use the method:

textview.set_monospace(monospace)

Signals

The commonly used signals of a TextView are:

"move-cursor" (textview, step, count, extend_selection)
"backspace" (textview)
"select-all" (textview)
"unselect-all" (textview)
"toggle-overwrite" (textview)
"toggle-cursor-visible" (textview)

The TextView emits the "move-cursor" signal when the text cursor is moved within the text field. This includes moving the cursor via the keyboard arrows, clicking with the mouse, and pressing Home and End. Using the event passes the textview on which the move occurred, the step value which indicates the type of move, the count which specifies the number of units moved and extend_selection which when True is specified shows that the selection was extended. A "backspace" event is caused to emit when the Backspace key is used. The "select-all" and "unselect-all" events happen when the user chooses to select or unselect all of the text. When the user indicates they want to switch between insert and overwrite mode, or vice-versa, the "overwrite-mode" event emits. Finally, the "toggle-cursor-visible" signal emits from the TextView whenever the cursor is shown or hidden.

Example

Below is an example of a TextView:

#!/usr/bin/env python3

from gi.repository import Gtk, Pango

def set_wrap_mode(radiobutton, wrap_mode):
    textview.set_wrap_mode(wrap_mode)

def set_style_text(checkbutton):
    start, end = textbuffer.get_bounds()

    if checkbuttonBold.get_active():
        textbuffer.apply_tag(texttagBold, start, end)
    else:
        textbuffer.remove_tag(texttagBold, start, end)

    if checkbuttonItalic.get_active():
        textbuffer.apply_tag(texttagItalic, start, end)
    else:
        textbuffer.remove_tag(texttagItalic, start, end)

    if checkbuttonUnderline.get_active():
        textbuffer.apply_tag(texttagUnderline, start, end)
    else:
        textbuffer.remove_tag(texttagUnderline, start, end)

window = Gtk.Window()
window.set_default_size(250, 300)
window.connect("destroy", Gtk.main_quit)

grid = Gtk.Grid()
window.add(grid)

scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
grid.attach(scrolledwindow, 0, 0, 2, 1)

textbuffer = Gtk.TextBuffer()
texttagBold = textbuffer.create_tag("Bold", weight=Pango.Weight.BOLD)
texttagItalic = textbuffer.create_tag("Italic", style=Pango.Style.ITALIC)
texttagUnderline = textbuffer.create_tag("Underline", underline=Pango.Underline.SINGLE)
textbuffer.set_text("GTK+, or the GIMP Toolkit, is a multi-platform toolkit for creating graphical user interfaces. Offering a complete set of widgets, GTK+ is suitable for projects ranging from small one-off tools to complete application suites.")

textview = Gtk.TextView(buffer=textbuffer)
textview.set_vexpand(True)
textview.set_hexpand(True)
scrolledwindow.add(textview)

radiobuttonWrapNone = Gtk.RadioButton(group=None, label="None")
radiobuttonWrapNone.connect("toggled", set_wrap_mode, Gtk.WrapMode.NONE)
grid.attach(radiobuttonWrapNone, 0, 1, 1, 1)

radiobuttonWrapChar = Gtk.RadioButton(group=radiobuttonWrapNone, label="Character")
radiobuttonWrapChar.connect("toggled", set_wrap_mode, Gtk.WrapMode.CHAR)
grid.attach(radiobuttonWrapChar, 0, 2, 1, 1)

radiobuttonWrapWord = Gtk.RadioButton(group=radiobuttonWrapNone, label="Word")
radiobuttonWrapWord.connect("toggled", set_wrap_mode, Gtk.WrapMode.WORD)
grid.attach(radiobuttonWrapWord, 0, 3, 1, 1)

radiobuttonWrapWordChar = Gtk.RadioButton(group=radiobuttonWrapNone, label="Word & Character")
radiobuttonWrapWordChar.connect("toggled", set_wrap_mode, Gtk.WrapMode.WORD_CHAR)
grid.attach(radiobuttonWrapWordChar, 0, 4, 1, 1)

checkbuttonBold = Gtk.CheckButton(label="Bold")
checkbuttonBold.connect("toggled", set_style_text)
grid.attach(checkbuttonBold, 1, 1, 1, 1)

checkbuttonItalic = Gtk.CheckButton(label="Italic")
checkbuttonItalic.connect("toggled", set_style_text)
grid.attach(checkbuttonItalic, 1, 2, 1, 1)

checkbuttonUnderline = Gtk.CheckButton(label="Underline")
checkbuttonUnderline.connect("toggled", set_style_text)
grid.attach(checkbuttonUnderline, 1, 3, 1, 1)

window.show_all()

Gtk.main()

Download: TextView