Tk entry and text widgets
last modified November 24, 2026 Tcl 8.6
Tk offers two text input widgets. The entry holds a single line, such as a name, a file path or a password. The text widget holds any number of lines and adds tags, marks, undo and search, so it can serve as a log viewer or a small editor.
Both widgets store their content internally and expose it through subcommands rather than through variables. This article shows how to read and change that content, how to style ranges of text, and how to assemble the pieces into a working editor window.
The entry widget
An entry is created with entry and filled with
insert. Text positions are integers starting at zero, and the
special index end points one character past the last one. The
get subcommand returns all or part of the content, while
delete removes a range and icursor moves the insertion
cursor.
package require Tk entry .user -width 26 pack .user -padx 10 -pady 6 .user insert end "jane@example.com" update puts "value: [.user get]" puts "end index: [.user index end]" .user delete 0 4 .user icursor 0 puts "edited: [.user get]" puts "cursor: [.user index insert]" entry .pass -show "*" -width 18 pack .pass -padx 10 -pady 6 .pass insert end "s3cret" update puts "password: [.pass get]" puts "mask: [.pass cget -show]" destroy .
Deleting the range 0 4 removed the four characters of the user
name and left the domain. The cursor was then placed at index 0, which
index insert reports. The second entry masks what it displays with
-show, yet get still returns the real text, so
masking is a display feature, not encryption.
Passwords and input checks
The -show option can be set at creation or changed later with
configure, which is how a "show password" check button is usually
implemented. Masking does not limit what the user types; to reject characters
use -validatecommand, described in
Tk variables and traces. Password values should
come from the user at run time, never from a literal in the script.
$ wish entry_demo.tcl value: jane@example.com end index: 16 edited: @example.com cursor: 0 password: s3cret mask: *
The text widget: insert and delete
The text widget uses a two-part index, line.character, where lines
start at 1 and characters at 0. Inserting text does not add line breaks by
itself, so every line has to end with \n. Deleting works on a range
of two indices, and the end index is exclusive.
package require Tk text .log -width 40 -height 5 pack .log -padx 8 -pady 8 .log insert end "build started\n" .log insert end "build finished\n" .log insert 1.end " ok" update puts "last index: [.log index end]" puts "line 1: [.log get 1.0 1.end]" .log delete 1.0 2.0 update puts "after delete, line 1: [.log get 1.0 1.end]" puts "after delete, line 2: '[.log get 2.0 2.end]'" destroy .
Appending ok at 1.end kept the text on the first
line, because that index means the end of line 1 without its newline. The
deletion 1.0 2.0 removed the first line together with the line
break, which moved the second line up. The last index was 4.0 before the
deletion because the widget always keeps a final newline; afterwards line 1
holds the remaining text and line 2 is empty.
$ wish text_insert.tcl last index: 4.0 line 1: build started ok after delete, line 1: build finished after delete, line 2: ''
Tags and tag configuration
A tag is a name attached to one or more ranges of text. Once a tag is
configured with display options such as -foreground,
-background or -font, every range carrying it is drawn
that way. This is how a log viewer colours warnings without changing the text.
package require Tk
text .report -width 40 -height 6
pack .report -padx 8 -pady 8
.report tag configure warn -foreground red -font {Helvetica 11 bold}
.report insert end "all services ok\n"
.report insert end "disk almost full\n" warn
.report insert end "backup done\n"
update
puts "tags at line 1: '[.report tag names 1.0]'"
puts "tags at line 2: '[.report tag names 2.0]'"
puts "warn colour: [.report tag cget warn -foreground]"
puts "warn ranges: [.report tag ranges warn]"
.report tag remove warn 2.0 3.0
puts "after remove: '[.report tag ranges warn]'"
destroy .
The third argument of insert applies a tag to the new text, which
is the quickest way to add a highlighted line. tag names lists the
tags at one position; tag ranges returns a flat list of start and
end indices for a tag. Because the newline belongs to the tagged text, the range
ends at 3.0, not at 2.end.
$ wish tags.tcl tags at line 1: '' tags at line 2: 'warn' warn colour: red warn ranges: 2.0 3.0 after remove: ''
Marks
A mark is a named position between two characters. Unlike an index such as
1.6, a mark moves when text is inserted or deleted before it, which
makes it suitable for bookmarks and for a cursor in a document. Its gravity
decides whether text inserted exactly at the mark lands before or after it.
package require Tk
text .doc -width 36 -height 4
pack .doc -padx 8 -pady 8
.doc insert end "alpha beta gamma"
.doc mark set spot 1.6
update
puts "spot: [.doc index spot]"
puts "text there: '[.doc get spot {spot + 4 chars}]'"
.doc mark gravity spot left
.doc insert spot ">>"
puts "after insert: [.doc get 1.0 {end - 1 char}]"
puts "spot now: [.doc index spot]"
.doc mark unset spot
puts "marks left: '[.doc mark names]'"
destroy .
The mark was placed at index 1.6, in front of beta, and the
get call confirms it with the four characters that follow. Setting
the gravity to left made the inserted >> land
after the mark, so the mark stayed at 1.6. Every text widget also keeps the
marks insert and current, which is why those two names
remain after mark unset; end is a special index, not a
mark.
$ wish marks.tcl spot: 1.6 text there: 'beta' after insert: alpha >>beta gamma spot now: 1.6 marks left: 'insert current'
Searching text
search scans the widget for a pattern and returns the index of the
first match, or an empty string. The -count option stores the match
length in a variable, and a loop that starts one character after each match
finds all occurrences. The stop index is positional and prevents Tk from
wrapping around at the end.
package require Tk
text .notes -width 40 -height 6
pack .notes -padx 8 -pady 8
.notes insert end "error: disk full\n"
.notes insert end "warning: memory low\n"
.notes insert end "error: network down\n"
set pos [.notes search -count len "error" 1.0 end]
while {$pos ne ""} {
puts "found '$pos' ($len chars)"
.notes tag add hit $pos "$pos + $len chars"
set pos [.notes search -count len "error" "$pos + 1 chars" end]
}
puts "hit ranges: [.notes tag ranges hit]"
puts "exact case: '[.notes search "Error" 1.0]'"
puts "nocase: '[.notes search -nocase "ERROR" 1.0]'"
destroy .
Each match is tagged hit, so the results stay visible after the
loop. By default the search is case sensitive: Error finds nothing
while -nocase finds the first line. Other switches are
-regexp for regular expressions, -backwards and
-elide; the Tcl regexp command page
explains the pattern syntax.
$ wish search_text.tcl found '1.0' (5 chars) found '3.0' (5 chars) hit ranges: 1.0 1.5 3.0 3.5 exact case: '' nocase: '1.0'
Undo
The text widget can manage an undo stack for you. With -undo 1
every edit is recorded; edit undo reverts one group and
edit redo reapplies it. Setting -autoseparators 0 and
calling edit separator gives control over where one group ends and
the next begins, which matters for bulk inserts.
package require Tk
text .edit -width 36 -height 4 -undo 1 -autoseparators 0 -maxundo 10
pack .edit -padx 8 -pady 8
.edit insert end "one"
.edit insert end " two"
puts "modified: [.edit edit modified]"
.edit edit separator
.edit insert end " three"
puts "text: [.edit get 1.0 {end - 1 char}]"
.edit edit undo
puts "undo 1: '[.edit get 1.0 {end - 1 char}]'"
.edit edit undo
puts "undo 2: '[.edit get 1.0 {end - 1 char}]'"
.edit edit redo
puts "redo: [.edit get 1.0 {end - 1 char}]"
.edit edit reset
puts "modified after reset: [.edit edit modified]"
puts "can undo now: [.edit edit canundo]"
destroy .
The first group held "one two", the second only " three", so the first undo
removed the last word and the second undo emptied the widget. redo
brought "one two" back. edit reset cleared the stack and
canundo became 0, but the modified flag stayed at 1;
edit modified 0 is the command that marks the document saved.
$ wish undo.tcl modified: 1 text: one two three undo 1: 'one two' undo 2: '' redo: one two modified after reset: 1 can undo now: 0
Scrollbars
A text widget shows a small part of its content, so it needs a scrollbar for the
rest. The two are connected in both directions: the widget calls the scrollbar
through -yscrollcommand, and the scrollbar calls the widget through
-command. Both exchange fractions between 0 and 1.
package require Tk
text .view -width 30 -height 5 -yscrollcommand {.sb set}
scrollbar .sb -orient vertical -command {.view yview}
pack .sb -side right -fill y
pack .view -side left -fill both -expand 1
for {set i 1} {$i <= 50} {incr i} {
.view insert end "line $i\n"
}
update
set range [.view yview]
puts "first visible: [.view index @0,0]"
puts "yview: [format {%.2f %.2f} [lindex $range 0] [lindex $range 1]]"
.view yview moveto 1.0
update
set range [.view yview]
puts "after moveto 1.0:"
puts "first visible: [.view index @0,0]"
puts "yview: [format {%.2f %.2f} [lindex $range 0] [lindex $range 1]]"
destroy .
yview returned 0.00 and 0.10: with 50 lines and 5 visible, the
top tenth of the document fills the window. @0,0 converts a pixel
position to a text index, here the first visible line. Scrolling to
1.0 moved the view to the bottom, where line 47 is at the top of
the five visible lines.
$ wish scrollbars.tcl first visible: 1.0 yview: 0.00 0.10 after moveto 1.0: first visible: 47.0 yview: 0.90 1.00
A small editor window
The final example assembles the widgets into an editor: a scrolling text area, two buttons and a status line. The grid weights let the text area grow when the window is resized, and the status line is a label bound to a variable, the pattern from the previous article.
package require Tk
set status "New document"
set words 0
proc refresh {} {
global status words
set text [.body get 1.0 {end - 1 char}]
set words [llength $text]
set status "[string length $text] char(s), $words word(s)"
}
proc clear_document {} {
.body delete 1.0 end
refresh
}
text .body -width 44 -height 8 -undo 1 -wrap word \
-yscrollcommand {.sb set}
scrollbar .sb -orient vertical -command {.body yview}
button .count -text "Count" -command refresh
button .clear -text "Clear" -command clear_document
label .status -textvariable status -anchor w -relief sunken
grid .body -row 0 -column 0 -sticky nsew
grid .sb -row 0 -column 1 -sticky ns
grid .count -row 1 -column 0 -sticky w -pady 4
grid .clear -row 1 -column 1 -sticky w -pady 4
grid .status -row 2 -column 0 -columnspan 2 -sticky ew
grid rowconfigure . 0 -weight 1
grid columnconfigure . 0 -weight 1
.body insert end "Shopping list\n"
.body insert end "coffee\n"
.body insert end "bread\n"
.body edit separator
.count invoke
update
puts "status: $status"
.body insert end "milk\n"
.count invoke
update
puts "status: $status"
.body edit undo
.count invoke
update
puts "after undo: $status"
.clear invoke
update
puts "status: $status"
destroy .
refresh measures the text without the empty last line that the text
widget always keeps, and clear_document deletes everything. A real
editor would load and save files with open and
gets; the Tcl gets command page shows the
line loop. The undo separator makes the bulk insert one step, so undo removes
only the line typed afterwards.
$ wish editor.tcl status: 27 char(s), 4 word(s) status: 32 char(s), 5 word(s) after undo: 27 char(s), 4 word(s) status: 0 char(s), 0 word(s)
Source
The entry and text manual pages document every subcommand used in this article, including the index forms, tags, marks, search and undo. The Tcl manual covers the string and list commands used to count words and characters.
This article covered single-line entry input, multi-line text, tags, marks, searching, undo and scrollbars, and joined them in a small editor. Continue with Tk listbox and treeview to display lists and tables, or revisit the Tcl string command for the text operations behind these widgets.
Author
List all Tcl Tutorials.