back next

The Tkinter Text Widget

The Text widget provides formatted text display. It allows you to display and edit text with various styles and attributes. The widget also supports embedded images and windows.

When to use the Text Widget

The text widget is used to display text documents, containing either plain text or formatted text (using different fonts, embedded images, and other embellishments). The text widget can also be used as a text editor.

Concepts

The text widget stores and displays lines of text.

The text body can consist of characters, marks, and embedded windows or images. Different regions can be displayed in different styles, and you can also attach event bindings to regions.

By default, you can edit the text widget’s contents using the standard keyboard and mouse bindings. To disable editing, set the state option to DISABLED (but if you do that, you’ll also disable the insert and delete methods).

Indexes

Indexes are used to point to positions within the text handled by the text widget. Like Python sequence indexes, text widget indexes correspond to positions between the actual characters.

Tkinter provides a number of different index types:

  • line/column (“line.column”)

  • line end (“line.end”)

  • INSERT

  • CURRENT

  • END

  • user-defined marks

  • user-defined tags (“tag.first”, “tag.last”)

  • selection (SEL_FIRST, SEL_LAST)

  • window coordinate (“@x,y”)

  • embedded object name (window, images)

  • expressions

Lines and columns

line/column indexes are the basic index type. They are given as strings consisting of a line number and column number, separated by a period. Line numbers start at 1, while column numbers start at 0, like Python sequence indexes. You can construct indexes using the following syntax:

    "%d.%d" % (line, column)

It is not an error to specify line numbers beyond the last line, or column numbers beyond the last column on a line. Such numbers correspond to the line beyond the last, or the newline character ending a line.

Note that line/column indexes may look like floating point values, but it’s seldom possible to treat them as such (consider position 1.25 vs. 1.3, for example). I sometimes use 1.0 instead of “1.0” to save a few keystrokes when referring to the first character in the buffer, but that’s about it.

You can use the index method to convert all other kinds of indexes to the corresponding line/column index string.

Line endings

A line end index is given as a string consisting of a line number directly followed by the text .end. A line end index correspond to the newline character ending a line.

Named indexes

INSERT (or “insert”) corresponds to the insertion cursor.

CURRENT (or “current”) corresponds to the character closest to the mouse pointer. However, it is only updated if you move the mouse without holding down any buttons (if you do, it will not be updated until you release the button).

END (or “end”) corresponds to the position just after the last character in the buffer.

User-defined marks are named positions in the text. INSERT and CURRENT are predefined marks, but you can also create your own marks. See below for more information.

User-defined tags represent special event bindings and styles that can be assigned to ranges of text. For more information on tags, see below.

You can refer to the beginning of a tag range using the syntax tag.first (just before the first character in the text using that tag), and tag.last (just after the last character using that tag).

    "%s.first" % tagname
    "%s.last" % tagname

If the tag isn’t in use, Tkinter raises a TclError exception.

The selection is a special tag named SEL (or “sel”) that corresponds to the current selection. You can use the constants SEL_FIRST and SEL_LAST to refer to the selection. If there’s no selection, Tkinter raises a TclError exception.

Coordinates

You can also use window coordinates as indexes. For example, in an event binding, you can find the character closest to the mouse pointer using the following syntax:

    "@%d,%d" % (event.x, event.y)

Embedded objects

Embedded object name can be used to refer to windows and images embedded in the text widget. To refer to a window, simply use the corresponding Tkinter widget instance as an index. To refer to an embedded image, use the corresponding Tkinter PhotoImage or BitmapImage object.

Expressions

Expressions can be used to modify any kind of index. Expressions are formed by taking the string representation of an index (use str if the index isn’t already a string), and appending one or more modifiers from the following list:

  • + count chars moves the index forward. The index will move over newlines, but not beyond the END index.

  • - count chars moves the index backwards. The index will move over newlines, but not beyond index “1.0”.

  • + count lines and - count lines moves the index full lines forward (or backwards). If possible, the index is kept in the same column, but if the new line is too short, the index is moved to the end of that line.

  • linestart moves the index to the first position on the line.

  • lineend the index to the last position on the line (the newline, that is).

  • wordstart and wordend moves the index to the beginning (end) of the current word. Words are sequences of letters, digits, and underline, or single non-space characters.

The keywords can be abbreviated and spaces can be omitted as long as the result is not ambigous. For example, + 5 chars can be shortened to +5c.

For compatibility with implementations where the constants are not ordinary strings, you may wish to use str or formatting operations to create the expression string. For example, here’s how to remove the character just before the insertion cursor:

    def backspace(event):
        event.widget.delete("%s-1c" % INSERT, INSERT)

Marks

Marks are (usually) invisible objects embedded in the text managed by the widget. Marks are positioned between character cells, and moves along with the text.

  • user-defined marks

  • INSERT

  • CURRENT

You can use any number of user-defined marks in a text widget. Mark names are ordinary strings, and they can contain anything except whitespace (for convenience, you should avoid names that can be confused with indexes, especially names containing periods). To create or move a mark, use the mark_set method.

Two marks are predefined by Tkinter, and have special meaning:

INSERT (or “insert”) is a special mark that is used to represent the insertion cursor. Tkinter draws the cursor at this mark’s position, so it isn’t entirely invisible.

CURRENT (or “current”) is a special mark that represents the character closest to the mouse pointer. However, it is only updated if you move the mouse without holding down any buttons (if you do, it will not be updated until you release the button).

Special marks can be manipulated as other user-defined marks, but they cannot be deleted.

If you insert or delete text before a mark, the mark is moved along with the other text. To remove a mark, you must use the mark_unset method. Deleting text around a mark doesn’t remove the mark itself.

If you insert text at a mark, it may be moved to the end of that text or left where it was, depending on the mark’s gravity setting (LEFT or RIGHT; default is RIGHT). You can use the mark_gravity method to change the gravity setting for a given mark.

In the following example, the “sentinel” mark is used to keep track of the original position for the insertion cursor.

    text.mark_set("sentinel", INSERT)
    text.mark_gravity("sentinel", LEFT)

You can now let the user enter text at the insertion cursor, and use text.get(sentinel, INSERT) to pick up the result.

Tags

Tags are used to associated a display style and/or event callbacks with ranges of text.

  • user-defined tags

  • SEL

You can define any number of user-defined tags. Any text range can have multiple tags, and the same tag can be used for many different ranges. Unlike the Canvas widget, tags defined for the text widget are not tightly bound to text ranges; the information associated with a tag is kept also if there is no text in the widget using it.

Tag names are ordinary strings, and they can contain anything except whitespace.

SEL (or “sel”) is a special tag which corresponds to the current selection, if any. There should be at most one range using the selection tag.

The following options are used with tag_config to specify the visual style for text using a certain tag.

background (color)

The background color to use for text having this tag.

Note that the bg alias cannot be used with tags; it is interpreted as bgstipple rather than background.

bgstipple (bitmap)

The name of a bitmap which is used as a stipple brush when drawing the background. Typical values are “gray12”, “gray25”, “gray50”, or “gray75”. Default is a solid brush (no bitmap).

borderwidth (distance)

The width of the text border. The default is 0 (no border).

Note that the bd alias cannot be used with tags.

fgstipple (bitmap)

The name of a bitmap which is used as a stipple brush when drawing the text. Typical values are “gray12”, “gray25”, “gray50”, or “gray75”. Default is a solid brush (no bitmap).

font (font)

The font to use for text having this tag.

foreground (color)

The color to use for text having this tag.

Note that the fg alias cannot be used with tags; it is interpreted as fgstipple rather than foreground.

justify (constant)

Controls text justification (the first character on a line determines how to justify the whole line). Use one of LEFT, RIGHT, or CENTER. Default is LEFT.

lmargin1 (distance)

The left margin to use for the first line in a block of text having this tag. Default is 0 (no left margin).

lmargin2 (distance)

The left margin to use for every line but the first in a block of text having this tag. Default is 0 (no left margin).

offset (distance)

Controls if the text should be offset from the baseline. Use a positive value for superscripts, a negative value for subscripts. Default is 0 (no offset).

overstrike (flag)

If non-zero, the text widget draws a line over the text that has this tag. For best results, you should use overstrike fonts instead.

relief (constant)

The border style to use for text having this tag. Use one of SUNKEN, RAISED, GROOVE, RIDGE, or FLAT. Default is FLAT (no border).

rmargin (distance)

The right margin to use for blocks of text having this tag. Default is 0 (no right margin).

spacing1 (distance)

Spacing to use above the first line in a block of text having this tag. Default is 0 (no extra spacing).

spacing2 (distance)

Spacing to use between the lines in a block of text having this tag. Default is 0 (no extra spacing).

spacing3 (distance)

Spacing to use after the last line of text in a block of text having this tag. Default is 0 (no extra spacing).

tabs (string)
underline (flag)

If non-zero, the text widget underlines the text that has this tag. For example, you can get the standard hyperlink look with (foreground=”blue”, underline=1). For best results, you should use underlined fonts instead.

wrap (constant)

The word wrap mode to use for text having this tag. Use one of NONE, CHAR, or WORD.

If you attach multiple tags to a range of text, style options from the most recently created tag override options from earlier tags. In the following example, the resulting text is blue on a yellow background.

 
    text.tag_config("n", background="yellow", foreground="red")
    text.tag_config("a", foreground="blue")
    text.insert(contents, ("n", "a"))

Note that it doesn’t matter in which order you attach tags to a range; it’s the tag creation order that counts.

You can change the tag priority using the tag_raise and tag_lower. If you add a text.tag_lower(“a”) to the above example, the text becomes red.

The tag_bind method allows you to add event bindings to text having a particular tag. Tags can generate mouse and keyboard events, plus Enter and Leave events. For example, the following code snippet creates a tag to use for any hypertext links in the text:

    text.tag_config("a", foreground="blue", underline=1)
    text.tag_bind("Enter>", show_hand_cursor)
    text.tag_bind("Leave>", show_arrow_cursor)
    text.tag_bind("Button-1>", click)
    text.config(cursor="arrow")

    text.insert(INSERT, "click here!", "a")

Patterns #

When you create a new text widget, it has no contents. To insert text into the widget, use the insert method and insert text at the INSERT or END indexes:

    text.insert(END, "hello, ")
    text.insert(END, "world")

You can use an optional third argument to the insert method to attach one or more tags to the newly inserted text:

    text.insert(END, "this is a ")
    text.insert(END, "link", ("a", "href"+href))

To insert embedded objects, use the window_create or image_create methods:

    button = Button(text, text="Click", command=click)
    text.window_create(INSERT, window=button)

To delete text, use the delete method. Here’s how to delete all text from the widget (this also deletes embedded windows and images, but not marks):

    text.delete(1.0, END)

To delete a single character (or an embedded window or image), you can use delete with only one argument:

    text.delete(INSERT)
    text.delete(button)

To make the widget read-only, you can change the state option from NORMAL to DISABLED:

    text.config(state=NORMAL)
    text.delete(1.0, END)
    text.insert(END, text)
    text.config(state=DISABLED)

Note that you must change the state back to NORMAL before you can modify the widget contents from within the program. Otherwise, calls to insert and delete will be silently ignored.

To fetch the text contents of the widget, use the get method:

    contents = text.get(1.0, END)

FIXME: add material on the dump method, and how to use it on 1.5.2 and earlier

Here’s a simple way to keep track of changes to the text widget:

    import md5
    def getsignature(contents):
        return md5.md5(contents).digest()

    text.insert(END, contents) # original contents
    signature = getsignature(contents)

    ...

    contents = text.get(1.0, END)
    if signature != getsignature(contents):
        print "contents have changed!"

FIXME: modify to handle ending linefeed added by text widget

The index method converts an index given in any of the supported formats to a line/column index. Use this if you need to store an “absolute” index.

    index = text.index(index)

However, if you need to keep track of positions in the text even after other text is inserted or deleted, you should use marks instead.

    text.mark_set("here", index)
    text.mark_unset("here")

The following function converts any kind of index to a (line, column)-tuple. Note that you can directly compare positions represented by such tuples.

 
    def getindex(text, index):
        return tuple(map(int, string.split(text.index(index), ".")))

    if getindex(text, INSERT)  getindex(text, "sentinel"):
        text.mark_set(INSERT, "sentinel")

The following example shows how to enumerate all regions in the text that has a given tag.

    ranges = text.tag_ranges(tag)
    for i in range(0, len(ranges), 2):
        start = ranges[i]
        stop = ranges[i+1]
        print tag, repr(text.get(start, stop))

The search method allows you to search for text. You can search for an exact match (default), or use a Tcl-style regular expression (call with the regexp option set to true).

    text.insert(END, "hello, world")

    start = 1.0
    while 1:
        pos = text.search("o", start, stopindex=END)
        if not pos:
            break
        print pos
        start = pos + "+1c"

Given an empty text widget, the above example prints 1.4 and 1.8 before it stops. If you omit the stopindex option, the search wraps around if it reaches the end of the text.

To search backwards, set the backwards option to true (to find all occurences, start at END, set stopindex to 1.0 to avoid wrapping, and use “-1c” to move the start position).

Reference #

Text(master=None, **options) (class) [#]

A display/editor widget for formatted text.

master
Parent widget.
**options
Widget options. See the description of the config method for a list of available options.

bbox(index) [#]

Calculates the bounding box for the given character.

This method only works if the text widget is updated. To make sure this is the case, you can call the update_idletasks method first.

index
Character index.
Returns:
A 4-tuple (x, y, width, height), or None, if the character is not visible.

compare(index1, op, index2) [#]

Compares two indexes. The op argument is one of “<”, “<=”, “==”, “>=”, “>”, or “!=” (Python’s “<>” syntax is not supported).

index1
First index.
op
Operator (see above).
index2
Second index.
Returns:
A true value if the condition is true.

config(**options) [#]

Modifies one or more widget options. If no options are given, the method returns a dictionary containing all current option values.

**options
Widget options.
autoseparators=
Default is 1. (autoSeparators/AutoSeparators)
background=
Default value is system specific. (the database name is background, the class is Background)
bg=
Same as background.
borderwidth=
Default value is 2. (borderWidth/BorderWidth)
bd=
Same as borderwidth.
cursor=
Default value is “xterm”. (cursor/Cursor)
exportselection=
Default value is 1. (exportSelection/ExportSelection)
font=
Default value is system specific. (font/Font)
foreground=
Default value is system specific. (foreground/Foreground)
fg=
Same as foreground.
height=
Default value is 24. (height/Height)
highlightbackground=
Default value is system specific. (highlightBackground/HighlightBackground)
highlightcolor=
Default value is system specific. (highlightColor/HighlightColor)
highlightthickness=
Default value is 0. (highlightThickness/HighlightThickness)
insertbackground=
Default value is system specific. (insertBackground/Foreground)
insertborderwidth=
Default value is 0. (insertBorderWidth/BorderWidth)
insertofftime=
Default value is 300. (insertOffTime/OffTime)
insertontime=
Default value is 600. (insertOnTime/OnTime)
insertwidth=
Default value is 2. (insertWidth/InsertWidth)
maxundo=
Default is 0. (maxUndo/MaxUndo)
padx=
Default value is 1. (padX/Pad)
pady=
Default value is 1. (padY/Pad)
relief=
Default value is SUNKEN. (relief/Relief)
selectbackground=
Default value is system specific. (selectBackground/Foreground)
selectborderwidth=
Default value is 0. (selectBorderWidth/BorderWidth)
selectforeground=
Default value is system specific. (selectForeground/Background)
setgrid=
Default value is 0. (setGrid/SetGrid)
spacing1=
Default value is 0. (spacing1/Spacing)
spacing2=
Default value is 0. (spacing2/Spacing)
spacing3=
Default value is 0. (spacing3/Spacing)
state=
Default value is NORMAL. (state/State)
tabs=
No default value. (tabs/Tabs)
takefocus=
No default value. (takeFocus/TakeFocus)
undo=
Default is 0. (undo/Undo)
width=
Default value is 80. (width/Width)
wrap=
Default value is CHAR. (wrap/Wrap)
xscrollcommand=
No default value. (xScrollCommand/ScrollCommand)
yscrollcommand=
No default value. (yScrollCommand/ScrollCommand)

debug(boolean=None) [#]

Enables or disables debugging.

boolean

delete(start, end=None) [#]

Deletes the character (or embedded object) at the given position, or all text in the given range. Any marks within the range are moved to the beginning of the range.

start
Start index.
end
End index. If omitted, only one character is deleted.

dlineinfo(index) [#]

Calculates the bounding box for the line containing the given character.

This method only works if the text widget is updated. To make sure this is the case, you can call the update_idletasks method first.

index
Character index.
Returns:
A 5-tuple: (x, y, width, height, offset). The last tuple member is the offset from the top of the line to the baseline. If the line is not visible, this method returns None.

dump(index1, index2=None, command=None, **kw) [#]

Dumps the widget contents.

edit_modified(arg=None) [#]

The edit_modified method.

edit_redo() [#]

The edit_redo method.

edit_reset() [#]

The edit_reset method.

edit_separator() [#]

The edit_separator method.

edit_undo() [#]

The edit_undo method.

get(start, end=None) [#]

Returns the character at the given position, or all text in the given range.

start
Start position.
end
End position. If omitted, only one character is returned.

image_cget(index, option) [#]

Returns the current value of the given option. If there’s no image on the given position, this method raises a TclError exception. Not implemented in Python 1.5.2 and earlier.

index
Index specifier.
option
Image option.
Returns:
Option value.

image_configure(index, **options) [#]

Modifies one or more image options. If there’s no image on the given position, this method raises a TclError exception. Not implemented in Python 1.5.2 and earlier.

index
Index specifier.
**options
Image options.
align=
image=
An image object. This must be a PhotoImage or BitmapImage object, or any compatible object.
name=
padx=
pady=

image_create(index, cnf={}, **kw) [#]

Inserts an image at the given position. The image must be a a Tkinter PhotoImage or BitmapImage instance (or an instance of the corresponding PIL classes).

This method doesn’t work with Tk versions before 8.0. As a workaround, you can put the image in a Label widget, and use window_create to insert the label widget.

index
Index specifier.
**options
Image options. See image_config for a complete list.

image_names() [#]

Finds the names of all images embedded in the text widget. Tkinter doesn’t provide a way to get the corresponding PhotoImage or BitmapImage objects, but you can keep track of those yourself using a dictionary (using str(image) as the key).

Returns:
A tuple containing image names.

index(index) [#]

Returns the “line.column” index corresponding to the given index.

index
Index specifier.
Returns:
The corresponding row/column, given as a “line.column” string.

insert(index, text, *tags) [#]

Inserts text at the given position. The index is typically INSERT or END. If you provide one or more tags, they are attached to the new text.

If you insert text on a mark, the mark is moved according to its gravity setting.

index
Where to insert the text.
text
The text to insert.
*tags
Optional tags to attach to the text.

mark_gravity(self, name, direction=None) [#]

Sets the gravity for the given mark. The gravity setting controls how to move the mark if text is inserted exactly on the mark. If LEFT, the mark is not moved if text is inserted at the mark (that is, the text is inserted just after the mark). If RIGHT, the mark is moved to the right end of the text (that is, the text is inserted just before the mark). The default gravity setting is RIGHT.

name
The name of the mark.
direction
The gravity setting (LEFT or RIGHT). If this argument is omitted, the method returns the current gravity setting.
Returns:
The current gravity setting, if direction was omitted.

mark_names() [#]

Finds the names of all marks used in the widget. This includes the INSERT and CURRENT marks (but not END, which is a special index, not a mark).

Returns:
A tuple containing mark names.

mark_next(index) [#]

The mark_next method.

index

mark_previous(index) [#]

The mark_previous method.

index

mark_set(name, index) [#]

Moves the mark to the given position. If the mark doesn’t exist, it is created (with gravity set to RIGHT). You also use this method to move the predefined INSERT and CURRENT marks.

name
Mark name.
index
New position.

mark_unset(name) [#]

Removes the given mark from the widget. You cannot remove the builtin INSERT and CURRENT marks.

name
Mark name.

scan_dragto(x, y) [#]

Scrolls the widget contents. The text view is moved 10 times the distance between the scanning anchor and the new position.

x
y

scan_mark(x, y) [#]

Sets the scanning anchor. This anchor is used for fast scrolling.

x
y

search(pattern, index, stopindex=None, forwards=None, backwards=None, exact=None, regexp=None, nocase=None, count=None) [#]

Searches for text strings or regular expressions.

pattern
index
stopindex
forwards
backwards
exact
regexp
nocase
count

see(index) [#]

Makes sure a given position is visible. If the index isn’t visible, scroll the view as necessary.

index
Index specifier.

tag_add(tagName, index1, *args) [#]

The tag_add method.

tagName
index1
*args

tag_bind(tagName, sequence, func, add=None) [#]

The tag_bind method.

tagName
sequence
func
add

tag_cget(tagName, option) [#]

The tag_cget method.

tagName
option

tag_config(tagName, cnf={}, **kw) [#]

The tag_config method.

tagName
cnf
**kw

tag_configure(tagName, cnf={}, **kw) [#]

The tag_configure method.

tagName
cnf
**kw

tag_delete(*tagNames) [#]

The tag_delete method.

*tagNames

tag_lower(tagName, belowThis=None) [#]

The tag_lower method.

tagName
belowThis

tag_names(index=None) [#]

The tag_names method.

index

tag_nextrange(tagName, index1, index2=None) [#]

The tag_nextrange method.

tagName
index1
index2

tag_prevrange(tagName, index1, index2=None) [#]

The tag_prevrange method.

tagName
index1
index2

tag_raise(tagName, aboveThis=None) [#]

The tag_raise method.

tagName
aboveThis

tag_ranges(tagName) [#]

The tag_ranges method.

tagName

tag_remove(tagName, index1, index2=None) [#]

The tag_remove method.

tagName
index1
index2

tag_unbind(tagName, sequence, funcid=None) [#]

The tag_unbind method.

tagName
sequence
funcid

window_cget(index, option) [#]

Returns the current value of the given window option. If there’s no window on the given position, this method raises a TclError exception.

index
option

window_config(index, **options) [#]

Modifies one or more window options. If there’s no window on the given position, this method raises a TclError exception.

index
**options
align=
create=
padx=
pady=
stretch=
window=
Window object.

window_configure(index, cnf=None, **kw) [#]

Same as window_config.

window_create(index, **options) [#]

Inserts a widget at the given position. You can either create the widget (which should be a child of the text widget itself) first, and insert it using the window option, or provide a create callback which is called when the window is first displayed.

index
Index specifier.
**options
Window options. See window_config for a complete list.

window_names() [#]

Returns a tuple containing all windows embedded in the text widget. In 1.5.2 and earlier, this method returns the names of the widgets, rather than the widget instances.

Here’s how to convert the names to a list of widget instances in a portable fashion:

windows = text.window_names()
try:
    windows = map(text.nametowidget, windows)
except TclError:
    pass

Returns:
A list of window names.

xview(*what) [#]

The xview method.

*what

xview_moveto(fraction) [#]

The xview_moveto method.

fraction

xview_scroll(number, what) [#]

The xview_scroll method.

number
what

yview(*what) [#]

The yview method.

*what

yview_moveto(fraction) [#]

The yview_moveto method.

fraction

yview_pickplace(*what) [#]

The yview_pickplace method.

*what

yview_scroll(number, what) [#]

The yview_scroll method.

number
what

 

A Django site. rendered by a django application. hosted by webfaction.