text
Guide: Text
Makes a text: a widget that shows and edits text of many lines, with tags, marks, images, and widgets.
text PATHNAME ?OPTION VALUE ...?
Description
Makes a text widget with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. The content is lines of characters. Tags give ranges of the content a look and bindings, marks keep places that move with the edits, and image create and window create put images and widgets into the lines.
Many widget commands take an index of a place in the content. An index is a base with optional changes after it. The base is one of these forms:
LINE.CHAR: line LINE from 1, and character CHAR from 0.LINE.endis the end of the line.end: the place after the last character. The content always ends with a newline.- A mark, such as
insertorcurrent. TAG.firstandTAG.last: the first character of TAG and the place after its last character.@X,Y: the character at the pixel X,Y.- The name of an image or a widget in the content.
A change is + N chars, - N chars, + N lines, - N lines, linestart, lineend, wordstart, or wordend, such as {1.0 lineend} or {end - 1 chars}. The units indices, displaychars, displayindices, and displaylines count places in other ways, and graphemes is an addition of ztk.
The tag sel is the selection, the mark insert is the insertion cursor, and the mark current follows the pointer. The parent of PATHNAME must exist, and PATHNAME must not. A text is a Text in CSS and for the option database.
Widget commands
| Command | Description |
|---|---|
text bbox | Gives the box of the character at INDEX. |
text compare | Compares two indices. |
text count | Counts the characters, the lines, or other units between two indices. |
text debug | Gives or sets the debug flag of the text widgets. |
text delete | Deletes the content from INDEX1 up to INDEX2. |
text dlineinfo | Gives the box of the display line of INDEX. |
text dump | Gives the text, the tags, the marks, the images, and the widgets of a range. |
text edit | Works on the undo stack and the modified flag. |
text get | Gives the characters from INDEX1 up to INDEX2. |
text image | Puts images into the content, and gives or sets their options. |
text index | Gives the LINE.CHAR form of an index. |
text insert | Inserts text at INDEX, with optional tags. |
text mark | Works on the marks: names for places between characters. |
text peer | Works on the peers of the text, in Tk. |
text pendingsync | Gives whether the layout of the lines has work to do. |
text replace | Replaces the content from INDEX1 up to INDEX2 with new text. |
text scan | Scrolls the text by a drag of the mouse. |
text search | Finds a pattern in the content and gives the index of the first match. |
text see | Scrolls the text so that INDEX is visible. |
text sync | Brings the layout of the lines up to date. |
text tag | Works on the tags: names for ranges of the content, with a look and bindings. |
text window | Puts widgets into the content, and gives or sets their options. |
text xview | Reads or changes the horizontal view of the text. |
text yview | Reads or changes the vertical view of the text. |
text cget | Gives the value of an option of the text. |
text configure | Gives or sets the options of the text. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-autoseparators | boolean | 1 | autoSeparators | Whether the text adds undo separators by itself. |
-background | color | #ffffff | background | The background color of the text. |
-blockcursor | boolean | 0 | blockCursor | Whether the insertion cursor is a block. |
-borderwidth | screen distance | 1 | borderWidth | The width of the border of the text. |
-cursor | cursor name | xterm | cursor | The shape of the pointer over the text. |
-endline | integer | empty | The last line of the content that the text shows, in Tk. | |
-exportselection | boolean | 1 | exportSelection | Whether the selection of the text is also the selection of the application. |
-font | font | TkFixedFont | font | The font of the text. |
-foreground | color | #000000 | foreground | The color of the text. |
-height | screen distance | 24 | height | The height of the text in lines of the font. |
-highlightbackground | color | #d9d9d9 | highlightBackground | The color of the focus ring while the text has no focus, in the look of Tk on X11. |
-highlightcolor | color | #000000 | highlightColor | The color of the focus ring while the text has the focus, in the look of Tk on X11. |
-highlightthickness | screen distance | 1 | highlightThickness | The width of the focus ring, in the look of Tk on X11. |
-inactiveselectbackground | color | #c3c3c3 | inactiveSelectBackground | The background color of the selection while the text has no focus. |
-insertbackground | color | #000000 | insertBackground | The color of the insertion cursor. |
-insertborderwidth | screen distance | 0 | insertBorderWidth | The width of the border of the insertion cursor. |
-insertofftime | integer | 300 | insertOffTime | The time in milliseconds that the insertion cursor is hidden in each blink. |
-insertontime | integer | 600 | insertOnTime | The time in milliseconds that the insertion cursor shows in each blink. |
-insertunfocussed | insertunfocussed | none | insertUnfocussed | How the insertion cursor shows while the text has no focus. |
-insertwidth | screen distance | 2 | insertWidth | The width of the insertion cursor. |
-maxundo | integer | 0 | maxUndo | The largest count of edits that the undo stack keeps. |
-padx | screen distance | 1 | padX | The space at the left and the right of the content. |
-pady | screen distance | 1 | padY | The space above and below the content. |
-relief | relief | sunken | relief | The 3-D look of the border. |
-selectbackground | color | #c3c3c3 | selectBackground | The background color of the selected text. |
-selectborderwidth | screen distance | 0 | selectBorderWidth | The width of the border around the selection. |
-selectforeground | color | #000000 | selectForeground | The text color of the selected text. |
-setgrid | boolean | 0 | setGrid | Whether the text sets a grid for the size of its toplevel. |
-spacing1 | screen distance | 0 | spacing1 | The space above each line of the text. |
-spacing2 | screen distance | 0 | spacing2 | The space between the display lines of a wrapped line. |
-spacing3 | screen distance | 0 | spacing3 | The space below each line of the text. |
-startline | integer | empty | The first line of the content that the text shows, in Tk. | |
-state | state | normal | state | Whether a script and the user can change the text. |
-tabs | string | empty | tabs | The tab stops of the text. |
-tabstyle | tabstyle | tabular | tabStyle | How the text finds the tab stop of a tab character. |
-takefocus | string | empty | takeFocus | Whether the Tab key can move the focus to the text. |
-undo | boolean | 0 | undo | Whether the text keeps an undo stack. |
-width | integer | 80 | width | The width of the text in average characters of the font. |
-wrap | wrap | char | wrap | How the text breaks a line longer than its width. |
-xscrollcommand | script | empty | xScrollCommand | A command prefix that the text calls when its horizontal view changes. |
-yscrollcommand | script | empty | yScrollCommand | A command prefix that the text calls when its vertical view changes. |
-autoseparators BOOLEAN
Whether the text adds undo separators by itself.
With -undo on and a true value, the text puts a separator in the undo stack at each change of the kind of edit, such as from typing to a deletion. With a false value, only edit separator adds separators.
% text .autoseparators -autoseparators 0
> .autoseparators cget -autoseparators
0% text .autoseparators2 -autoseparators maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-background COLOR
The background color of the text.
The color behind the content. -bg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-background {#fde68a}
-background {#bfdbfe}-width 24 -height 3.% text .background -background {#336699}
> .background cget -background
#336699% text .background2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-blockcursor BOOLEAN
Whether the insertion cursor is a block.
In Tk, a true value draws the insertion cursor as a block over a character. ztk stores the value with no effect: the cursor is a bar.
% text .blockcursor -blockcursor 1
> .blockcursor cget -blockcursor
1% text .blockcursor2 -blockcursor maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-borderwidth DISTANCE
The width of the border of the text.
The width of the border around the text. -bd is the same option. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

-borderwidth 0
-borderwidth 1
-borderwidth 4-width 24 -height 3 -relief solid.% text .borderwidth -borderwidth 4
> .borderwidth cget -borderwidth
4% text .borderwidth2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-cursor CURSOR
The shape of the pointer over the text.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. A value that starts with @ names a file. An empty value uses the cursor of the parent.
% text .cursor -cursor hand2
> .cursor cget -cursor
hand2% text .cursor2 -cursor nocursor
bad cursor spec "nocursor"
-errorcode TK VALUE CURSOR-endline LINE
The last line of the content that the text shows, in Tk.
In Tk, the text shows and gives only the lines up to this line of its content, for a peer. An empty value uses the last line. ztk stores the value with no effect.
% text .endline -endline 5
> .endline cget -endline
5% text .endline2 -endline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-exportselection BOOLEAN
Whether the selection of the text is also the selection of the application.
In Tk, a true value makes a selection in the text the PRIMARY selection, and selection get gives it. ztk stores the value with no effect: the selection of the text is not the PRIMARY selection.
% text .exportselection -exportselection 0
> .exportselection cget -exportselection
0% text .exportselection2 -exportselection maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-font FONT
The font of the text.
A font name, such as TkDefaultFont, or a font description, such as {Helvetica 12 bold}. A family that does not exist falls back to a font of the system.

-font TkDefaultFont
-font TkFixedFont
-font {Helvetica 16 bold}-width 24 -height 3.% text .font -font {Helvetica 12 bold}
> .font cget -font
Helvetica 12 bold% text .font2 -font {Helvetica big}
expected integer but got "big"
-errorcode TCL VALUE NUMBER-foreground COLOR
The color of the text.
The color of the text. -fg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-foreground {#b91c1c}
-foreground {#2563eb}-width 24 -height 3.% text .foreground -foreground {#336699}
> .foreground cget -foreground
#336699% text .foreground2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-height LINES
The height of the text in lines of the font.
The text asks for room for this count of lines of -font.

-height 2
-height 4-width 24.% text .height -height 4
> .height cget -height
4% text .height2 -height x
bad screen distance "x"
-errorcode TK VALUE PIXELS-highlightbackground COLOR
The color of the focus ring while the text has no focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the text does not have the focus. ztk stores the value with no effect.
% text .highlightbackground -highlightbackground {#336699}
> .highlightbackground cget -highlightbackground
#336699% text .highlightbackground2 -highlightbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightcolor COLOR
The color of the focus ring while the text has the focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the text has the focus. ztk stores the value with no effect.
% text .highlightcolor -highlightcolor {#336699}
> .highlightcolor cget -highlightcolor
#336699% text .highlightcolor2 -highlightcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightthickness DISTANCE
The width of the focus ring, in the look of Tk on X11.
The width of the ring around the text that shows the focus. The modern theme draws the focus with the stylesheet.
% text .highlightthickness -highlightthickness 4
> .highlightthickness cget -highlightthickness
4% text .highlightthickness2 -highlightthickness x
bad screen distance "x"
-errorcode TK VALUE PIXELS-inactiveselectbackground COLOR
The background color of the selection while the text has no focus.
An empty value hides the selection while the text has no focus.
% text .inactiveselectbackground -inactiveselectbackground {#336699}
> .inactiveselectbackground cget -inactiveselectbackground
#336699% text .inactiveselectbackground2 -inactiveselectbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-insertbackground COLOR
The color of the insertion cursor.
The color of the bar that marks the place of the next typed character.
% text .insertbackground -insertbackground {#336699}
> .insertbackground cget -insertbackground
#336699% text .insertbackground2 -insertbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-insertborderwidth DISTANCE
The width of the border of the insertion cursor.
In Tk, the insertion cursor can have a 3-D border of this width. ztk stores the value with no effect.
% text .insertborderwidth -insertborderwidth 4
> .insertborderwidth cget -insertborderwidth
4% text .insertborderwidth2 -insertborderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-insertofftime N
The time in milliseconds that the insertion cursor is hidden in each blink.
0 stops the blink, and the cursor stays on.
% text .insertofftime -insertofftime 5
> .insertofftime cget -insertofftime
5% text .insertofftime2 -insertofftime x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-insertontime N
The time in milliseconds that the insertion cursor shows in each blink.
The cursor blinks only while the text has the focus and -insertofftime is more than 0.
% text .insertontime -insertontime 5
> .insertontime cget -insertontime
5% text .insertontime2 -insertontime x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-insertunfocussed WORD
How the insertion cursor shows while the text has no focus.
In Tk, none hides the cursor, hollow draws an outline, and solid draws the cursor as with the focus. ztk stores the value with no effect.
Values: hollow, none, solid.
% text .insertunfocussed -insertunfocussed hollow
> .insertunfocussed cget -insertunfocussed
hollow% text .insertunfocussed2 -insertunfocussed bogus
bad insertunfocussed "bogus": must be hollow, none, or solid
-errorcode TCL LOOKUP INDEX insertunfocussed bogus-insertwidth DISTANCE
The width of the insertion cursor.
A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.
% text .insertwidth -insertwidth 4
> .insertwidth cget -insertwidth
4% text .insertwidth2 -insertwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-maxundo N
The largest count of edits that the undo stack keeps.
0 or less keeps each edit. A separator ends one edit.
% text .maxundo -maxundo 5
> .maxundo cget -maxundo
5% text .maxundo2 -maxundo x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-padx DISTANCE
The space at the left and the right of the content.
The space between the text and the left and the right sides. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

-padx 1
-padx 8
-padx 16-width 24 -height 3.% text .padx -padx 4
> .padx cget -padx
4% text .padx2 -padx x
bad screen distance "x"
-errorcode TK VALUE PIXELS-pady DISTANCE
The space above and below the content.
The space between the text and the top and the bottom. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

-pady 1
-pady 8
-pady 16-width 24 -height 3.% text .pady -pady 4
> .pady cget -pady
4% text .pady2 -pady x
bad screen distance "x"
-errorcode TK VALUE PIXELS-relief RELIEF
The 3-D look of the border.
Each value side by side: the contact sheet of the reliefs
The style of the border: raised and sunken look 3-D, and groove and ridge are lines. The theme draws the border, and the CSS of the theme can give a relief its own look with border-style.
Values: flat, groove, raised, ridge, solid, sunken.

-relief flat
-relief raised
-relief sunken
-relief groove
-relief ridge
-relief solid-width 24 -height 3 -borderwidth 4. The modern theme draws each relief but flat with the same border.% text .relief -relief groove
> .relief cget -relief
groove% text .relief2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy-selectbackground COLOR
The background color of the selected text.
The color behind the characters of the selection. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-selectbackground {#fde68a}
-selectbackground {#bfdbfe}-width 24 -height 3.% text .selectbackground -selectbackground {#336699}
> .selectbackground cget -selectbackground
#336699% text .selectbackground2 -selectbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-selectborderwidth DISTANCE
The width of the border around the selection.
In Tk, the selection can have a 3-D border of this width. ztk stores the value with no effect.
% text .selectborderwidth -selectborderwidth 4
> .selectborderwidth cget -selectborderwidth
4% text .selectborderwidth2 -selectborderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-selectforeground COLOR
The text color of the selected text.
The color of the characters of the selection.

-selectforeground {#b91c1c}
-selectforeground {#2563eb}-width 24 -height 3.% text .selectforeground -selectforeground {#336699}
> .selectforeground cget -selectforeground
#336699% text .selectforeground2 -selectforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-setgrid BOOLEAN
Whether the text sets a grid for the size of its toplevel.
In Tk, a true value makes the window manager resize the toplevel in steps of one line and one character. ztk stores the value with no effect.
% text .setgrid -setgrid 1
> .setgrid cget -setgrid
1% text .setgrid2 -setgrid maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-spacing1 DISTANCE
The space above each line of the text.
The space above the first display line of each line of the text. A tag can set its own value.

-spacing1 0
-spacing1 8-width 24 -height 3.% text .spacing1 -spacing1 4
> .spacing1 cget -spacing1
4% text .spacing12 -spacing1 x
bad screen distance "x"
-errorcode TK VALUE PIXELS-spacing2 DISTANCE
The space between the display lines of a wrapped line.
The space between two display lines of one line that wraps. A tag can set its own value.
% text .spacing2 -spacing2 4
> .spacing2 cget -spacing2
4% text .spacing22 -spacing2 x
bad screen distance "x"
-errorcode TK VALUE PIXELS-spacing3 DISTANCE
The space below each line of the text.
The space below the last display line of each line of the text. A tag can set its own value.
% text .spacing3 -spacing3 4
> .spacing3 cget -spacing3
4% text .spacing32 -spacing3 x
bad screen distance "x"
-errorcode TK VALUE PIXELS-startline LINE
The first line of the content that the text shows, in Tk.
In Tk, the text shows and gives only the lines from this line of its content, for a peer. An empty value uses the first line. ztk stores the value with no effect.
% text .startline -startline 2
> .startline cget -startline
2% text .startline2 -startline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-state STATE
Whether a script and the user can change the text.
disabled blocks insert, delete, replace, and the keys of the user, so the text is read only. normal allows each change.
Values: disabled, normal.
# A disabled text keeps its content.
% text .state -state disabled
> .state insert end abc
> .state get 1.0 end-1c% text .state2 -state busy
bad state "busy": must be disabled or normal
-errorcode TCL LOOKUP INDEX state busy-tabs LIST
The tab stops of the text.
A list of distances, each one with an optional alignment after it: left, right, center, or numeric. An empty value puts a tab stop at each 8 average characters.
% text .tabs -tabs {2c left 4c right}
> .tabs cget -tabs
2c left 4c rightztk stores each value with no check. Tk 8.6.16 gives the error bad screen distance for a bad distance.
-tabstyle WORD
How the text finds the tab stop of a tab character.
In Tk, tabular gives each tab character of a line its own tab stop, and wordprocessor uses the next tab stop after the place of the tab character. ztk stores the value with no effect.
Values: tabular, wordprocessor.
% text .tabstyle -tabstyle wordprocessor
> .tabstyle cget -tabstyle
wordprocessor% text .tabstyle2 -tabstyle bogus
bad tabstyle "bogus": must be tabular or wordprocessor
-errorcode TCL LOOKUP INDEX tabstyle bogus-takefocus TEXT
Whether the Tab key can move the focus to the text.
1 puts the text in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk for the widget. In Tk the value can also be a script, and its result is the answer. ztk stores each value with no check.
% text .takefocus -takefocus 1
> .takefocus cget -takefocus
1ztk stores each value of the option with no check.
-undo BOOLEAN
Whether the text keeps an undo stack.
With a true value, each change goes on the undo stack, and edit undo and edit redo work.
% text .undo -undo 1
> .undo insert end hello
> .undo edit undo
> .undo get 1.0 end-1c% text .undo2 -undo maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-width CHARS
The width of the text in average characters of the font.
The text asks for room for this count of characters of -font.

-width 12
-width 24-height 3.% text .width -width 5
> .width cget -width
5% text .width2 -width x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-wrap WORD
How the text breaks a line longer than its width.
char breaks a line at any character, word breaks it at a space, and none breaks no line, so a long line goes past the right side.
Values: char, none, word.

-wrap none
-wrap char
-wrap word-width 14 -height 4.% text .wrap -wrap none
> .wrap cget -wrap
none% text .wrap2 -wrap bogus
bad wrap "bogus": must be char, none, or word
-errorcode TCL LOOKUP INDEX wrap bogus-xscrollcommand SCRIPT
A command prefix that the text calls when its horizontal view changes.
The text calls the prefix with two fractions, the first and the last visible part of the content, from 0 to 1. The usual value is {.sb set} for a scrollbar .sb.
% text .xscrollcommand -xscrollcommand {set ::done 1}
> .xscrollcommand cget -xscrollcommand
set ::done 1ztk stores each value of the option with no check.
-yscrollcommand SCRIPT
A command prefix that the text calls when its vertical view changes.
The text calls the prefix with two fractions, the first and the last visible part of the content, from 0 to 1. The usual value is {.sb set} for a scrollbar .sb.
% text .yscrollcommand -yscrollcommand {set ::done 1}
> .yscrollcommand cget -yscrollcommand
set ::done 1ztk stores each value of the option with no check.
Returns
PATHNAME.
Examples
% text .notes -width 40 -height 10
> .notes insert end "First line\nSecond line"
> .notes get 2.0 2.end
Second line
% text .styled
> .styled insert end "Warning: " bold "disk full"
> .styled tag configure bold -font {Helvetica 12 bold}
> .styled tag ranges bold
1.0 1.9
% winfo class [text .go]
Text
Errors
% text .x -foo 1
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo
% text .twice
> text .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% text .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% text
wrong # args: should be "text pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
peer namesgives an empty list, andpeer creategives the errorthe text subcommand "peer create" is not in ztk.-startlineand-endlineare stored with no effect: the text shows and gives each line of its content.-blockcursor,-insertunfocussed,-insertborderwidth,-setgrid,-tabstyle,-highlightbackground, and-highlightcolorare stored with no effect.-tabsaccepts each value. Tk 8.6.16 gives the errorbad screen distancefor a bad distance.tag removeof a tag that does not exist makes no tag. Tk 8.6.16 makes the tag.- The text moves, deletes, and wraps by grapheme cluster, where Tk 8.6.16 steps by character. The index unit
graphemesis an addition of ztk. - An error of a widget command has the
-errorcodeNONE. Tk 8.6.16 gives a code such asTK TEXT BAD_INDEX,TK LOOKUP TEXT_TAG, orTCL WRONGARGS.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1