The Tk Text Widget

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

Text formatting and image and widget presentation is the top layer of the text widget's box of tricks. Tags provide an extensive array of facilities that can add intriguing dynamic abilities to pieces of text.

In order to make some of the following concepts a little clearer, another small demonstration script is provided in Listing 2. This script creates a text widget and loads it with the contents of an XML file. Areas of the text are tagged according to their position in the XML structure. Figure 3 shows this script running with my Konqueror bookmarks file as its input. Figure 3 here


The text widget has its own built in search mechanism. A simple search command looks like this:

.t search $searchText 1.0 end

This command returns the index of the first occurrence of the string in $searchText between the start of the text (index 1.0) and the end (index end). Implementing a find next feature is as simple as changing the starting position for the search to the location the last find returned. Searches can be configured to go forwards or backwards, be case insensitive or be made to look for regular expressions as opposed to fixed strings.

Tag and Bind

All of the above demonstrates that the Tk text widget is a flexible solution to most text formatting and editing requirements. More power is waiting to be explored, though, and this is where the programmer needs to start employing some imagination. This extra power is opened up with the text widget's bind subcommand.

The bind subcommand associates a Tcl script with the occurrence of an X event within a text widget often, but not necessarily, over a tagged area of text. A simple example might be a mouse over; when the X system reports that the mouse pointer has been placed over a piece of tagged text, we might want to change the mouse pointer shape. Code to bind such an event looks like this:

.t tag bind mouseover <Enter> {.t configure -cursor center_ptr}
.t tag bind mouseover <Leave> {.t configure -cursor xterm}

The X <Enter> event is received when the mouse pointer enters a certain screen location, and the <Leave> event is received when it leaves it. In this case the screen location is any area of text in the .t widget is tagged with the mouseover tag. In this simple example, the script run in each case is a one-liner that reconfigures the text widget with a new mouse pointer shape. In a more advanced example, perhaps some bubble help would appear or an acronym would be expanded or maybe more details could be presented from a database lookup. The only limitation is what can be achieved in the fairly limited timeframe. Given modern hardware and fast scripting languages, such as Tcl, Perl or Python, a surprisingly large amount of code can be executed within a mouse movement.


All X events can be utilised by the bind command, including mouse clicks. An obvious use for this is a hyperlink. Setting up the binding is as simple as before:

.t tag bind hyperlink <Button-1> {clickLink %x %y}

Now, whenever mouse button 1 is clicked over any text tagged with the hyperlink tag, the Tcl procedure clickLink is called. The %x and %y are replaced by the bind mechanism with the pixel coordinates of where the mouse click happened. This allows us to find the clicked piece of text. The clickLink procedure, which assumes the clicked text contains a URL, looks like this:

proc clickLink { xpos ypos } {

    set i [.t index @$xpos,$ypos]
    set range [.t tag prevrange hyperlink $i]
    set url [eval .t get $range]
    catch { exec mozilla $url & }

This code finds the index of the character clicked on using the @x,y syntax of the text widget's index subcommand. It uses that index to find the range of text covered by that occurrence of the hyperlink tag. It then gets that piece of text--the URL--and hands it over to, in this case, Mozilla. The catch command prevents the script from stopping if there's a problem starting Mozilla. I've omitted error handling for simplicity; in a real example a problem with Mozilla would need to be handled smoothly. Witness the power of this tag-and-bind approach--hyperlinks implemented in less than 10 lines of code.

Hyperlinks are a fairly obvious use for this functionality, so let's consider some alternatives. Imagine an e-mail client in which the user clicks on a subject line in one text widget and sees the text of the e-mail in another. Imagine a programmer's editor where you can click on a keyword or function and see a summary of the syntax it requires. How about a database front end, where the user hovers the mouse pointer over a table name and sees the structure of that table? These sorts of features are simple to implement with the tag and bind approach.



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."