The Tk Text Widget

The powerful text widget in the Tk toolkit offers many facilities to writers of Tcl, Perl and Python scripts.

A few problems can be found in using the Tk text widget. When a tool has been around for as long as this one, the number of problems become fairly small. There is one significant problem with the widget as it stands, however. In order to get the full benefits of margin handling and the like, text needs to be inserted without newline characters. That is, each paragraph needs to be one long string with a newline at the end. This allows the word wrapping to work properly. However, the widget's default keyboard navigation behaviour is that pressing the up or down arrow key moves up or down a real line, rather than a displayed line. The effect is to skip up or down a paragraph at a time instead of a line, and so the only way to move to the middle of a paragraph is to hold the right arrow key and allow the cursor to work its way across and downwards.

Given the quality of thought and implementation that clearly has gone into the rest of the widget, it's hard to see how this behaviour has been tolerated for so long. The problem finally was addressed for Tk 8.5, which, as of this writing, is in alpha release. In the meantime, the Tcler's Wiki has a workaround that installs more sensible behaviour, see Resources.


The Tk text widget is a remarkably powerful tool available to script writers who work with Tcl, Python and Perl. In its basic form it allows multi-line text entry, with rich text formatting and undo facilities where appropriate. Its more powerful features provide a framework for implementing all sorts of interesting facilities, including image handling, embedded GUI controls, mouse overs and hyperlinks. If your application involves the manipulation or presentation of textual information, the abilities of Tk's text widget should not be overlooked.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: The Tk Text Widget

Anonymous's picture

Error in startup script: invalid command name "tk_TextSetCursor"
while executing
"tk_TextSetCursor .t 1.0"
(file "textdemo.tcl" line 63)
What version of Tk is required for the textdemo.tcl script ?

Watch the angle brackets

Anonymous's picture

I spent about 20 minutes trying to get the tag binding code to work before I realized that the <Button-1> stuff was missing. It's in the HTML source, though, you just need to escape it. (As does this text entry box, apparently)

Not just Tcl, Perl, and Python.

techentin's picture

The Tk Text widget is available to scripters, INCLUDING those working in Perl, Python, and Tcl, but not restricted to them. Users of Ruby, PHP, Scheme, and other languages have access to the Text widget. Although Tcl/Tk, Tkinter, and Perl/Tk are the most mature.

Perl/Tk is not only one for Tk for Perl

Vadim Konovalov's picture

Perl/Tk has connection from Perl to Tk, while Tcl is striped out in this approach
CPAN module Tcl::Tk is another approach that connects to Tk via Tcl. This approach is more flexible and better than one from perl/Tk, see for comments.
To mention, Ruby, Python (dont know about others) do also this way (via Tcl)

Re: Not just Tcl, Perl, and Python.

Anonymous's picture

ruby/tk has been included with the interpreter for years, I think it's quite mature now..

Re: The Tk Text Widget

Anonymous's picture

Nice article which demonstrates well how a small body of code can do very useful things with the text widget.
However, there is an error in presentation that should be fixed ASAP:

.t tag configure title -font "helvetica 14"
-justify "center"
-underline on

Is broken Tcl code. The first two lines need to end with
backslash. As is it Tcl will complain about "-justify" being an
unknown command!

Re: The Tk Text Widget

derekfountain's picture

That's true; I missed that. The original text was:

.t tag configure title -font "helvetica 14"
-justify "center"
-underline on

There's also a (harmless) edit in another code snippet. The text insertion listing originally said this:

.t insert 1.0 "The Legend of Black Cave
" title

The LJ formatting process replaced the
for a literal newline.

Re: The Tk Text Widget

derekfountain's picture

Hmm, that's interesting. I previewed my comment to ensure it would look OK and it did. On the live page the slashes disappear again! It should be one long line; breaking with slashes was just for formatting.

.t tag configure title -font "helvetica 14" -justify "center" -underline on

.t insert 1.0 "The Legend of Black Cave
" title

Re: The Tk Text Widget

Anonymous's picture

The Tk text widget really is a fantastic thing -- powerful enough even for entire text editors or web browsers to be built on top of! Yet, in Tk 8.5 it has become even better!

Editing in the Caveats section

derekfountain's picture

The first two sentences in the "Caveats" section have been edited slightly by Linux Journal. The changes are minor, but I feel they significantly alter the feel of what I said. For the record, the original text was:

"There are very few problems with the Tk text widget. When a tool has been around for as long as this one, it gets honed closer to perfection with each release."

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Upcoming Webinar
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
11am CDT, April 29th
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot