Tcl Programming Examples, Tutorials, and More

Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more

Tcl, or "Tool Command Language" (and pronounced "Tickle"), is a dynamically-typed scripting language originally invented in the late 1980s. It is often used for prototyping and embedded systems, but it is quite capable enough to be used for full-scale applications. It was influenced by, and bears some similarities to, Lisp.

Tk is a closely-related GUI tool kit. It is an extension to Tcl, but is now available for use with several other languages.

Tcl and Tk were created by the same programmer, and are frequently used as if they were components of a single tool. They even share version numbers. Together they are usually referred to as "Tcl/Tk" and they are a powerful technology for rapid development.

About the Tcl language

Tcl is an interesting language; it has a number of somewhat unusual features, compared to other programming languages.

Tcl has no reserved words or control structures. Control structures such as "for" and "if" are actually commands. Commands in Tcl can be overridden or redefined, so a programmer could literally change what "for" (or any other language construct) means in a program. While this is not usually advisable, it is very powerful.

All the data types in Tcl can be treated as strings because, in an important way, they are strings. Even source code can be manipulated as a string. The integer 2 and the string "2" are the same value. This doesn't mean that all variables have to be treated as strings all the time. The value 2 is an integer when passed as an argument to a command that is meaningful for integers. If "hello" is passed to a command that treats the argument like an integer, the result will either be nonsense or an error. It is up to the programmer to make sure that the right values are passed as arguments.

This means that Tcl values are fundamentally polymorphic. Additionally, code can be generated, inspected, and manipulated like any other data. These features enable powerful introspection and metaprogramming techniques.


The syntax of the language is very simple, and resembles the Bash scripting language: an unadorned command followed by one or more arguments, separated by spaces. Curly brackets are used to set off arguments which need to include spaces in them (that is, inside braces, spaces are not word delimiters).

# This is a comment. Comments begin with the hash sign.

# a very simple command to print the word "hello"
puts hello

# but if we want to say "hello world"
puts {hello world}

Much like a shell script, a Tcl script is simply a series of single-line commands. The interpreter always reads the first line as a command name, and treats each additional item on the line as an argument to that command.

Even seemingly complex control structures work this way. Consider the following:

if { $a == 1 } { puts {Hello, World} } { puts { Goodbye, cruel world! } }

The command being invoked is if. The first argument is an expression to be evaluated. The next argument is a script to be run if the first argument is true. The last argument is a script to be run if the first argument wasn't true.

This is, of course, pretty basic programming, but it's interesting how the interpreter handles this syntax. Also, if the expressions and scripts were single words, the curly brackets would be unneeded:

# create two new procedures
proc hello {} {puts { Hello, world! } }
proc goodbye {} {puts {Goodbye, cruel world!}

# conditional with no curly brackets
if $a hello goodbye

Along with the curly brackets, there are two additional "grouping" tokens: double quotes and square brackets.

Double quotes and curly brackets are similar (since everything is a string) except that double quotes interpolate variables, whereas curly braces treat their contents as literal strings.

# assign "world" to the variable a
set a world

puts $a
# world

puts {hello $a}
# hello $a

puts "hello $a"
# hello world

Square brackets are used for command substitution. That is, the contents of a pair of square brackets is first evaluated by the interpreter, and then the return value is substituted into the line as either a command or argument.

puts [expr {1 + 1}]
# 2

# A silly proc that just returns the string "puts"
proc returns_puts {} { return puts }

# The return value become a command
[returns_puts] [expr {1 + 1}]
# 2

Additionally, since commands are just strings, variable values can be commands, or entire lines.

# Assign a string "puts {this is a string}" to foo
set foo {puts {this is a string} }

# this is a string

puts $foo
# puts {this is a string}

As you can see, Tcl is very flexible.

Tcl Tutorials

Books on Tcl/Tk

  • Practical Programming in Tcl and Tk (2003) by Welch and Jones is an authoritative textbook on both Tcl and Tk, with enough in-depth coverage to satisfy even experienced programmers coming from other languages;
  • Tcl/Tk, Second Edition: A Developer's Guide (2003) by Clif Flynt is a very practical book covering both the language and also the development ecosystem, with specific coverage on tooling, commonly used extensions, and development practices;
  • Tcl and the Tk Toolkit (2009) by Oesterhout and Jones is the definitive guide on the language, co-authored by its inventor;
  • Tcl/Tk 8.5 Programming Cookbook (2011) by Bert Wheeler provides a number of practical solutions to common problems in Tcl/Tk programming.

Tk — The GUI Tool Kit for Tcl

There are a lot of reasons that Tcl became popular when it did, but Tk is probably the most important one. Tk is short for Toolkit. It makes it easy to build graphical user interfaces (GUIs) with Tcl.

Tk provides a relatively easy set of commands for generating a form-based GUI, with all the usual desktop elements you might expect: buttons, input boxes, labels, various types of selectors, scrollbars, and so forth. These elements are specified in your source code, along with the executable code that hooks into the widgets — for example, what function should run when a button is pressed, or where a list of options for a selection menu comes from. While the syntax is different, you'll be familiar with this process if you've ever worked with HTML/JS applications or forms.

Tk was originally developed as an extension to Tcl, by the same person who invented Tcl. But Tcl quickly became a cross-platform library, available to programmers in dozens of languages. Tk was released publicly in 1991, and Python's Tkinter module was released in 1994. PerlTk came out shortly after that.

Prior to the 8.5 release of Tcl/Tk, there was only one visual design controlling how these elements actually looked. Now there are themes for major operating systems, so that an application built in Tk looks native to the platform it is running on.

When Tk first became available, it was one of the easiest and fastest ways to get a desktop GUI application up and running. Even though it was only available for Tcl and Unix at first, it took off because it was so simple to use. There are a lot more options today than we did back then, but Tk is still one of the most efficient ways to rapidly prototype and develop desktop applications.

Tk Tutorials

Several of the Tcl tutorials mentioned above include information on Tk. The following focus on it specifically.

Should I learn Tcl?

Tcl is a great language. It has passed its peak in popularity, but there is still a very active developer community and plenty of Tcl code out in the world in need of looking after.

More importantly, it really is different. If you've moved from JavaScript to PHP to Ruby to Python, you keep retreading very similar conceptual ground. You're learning new syntax and new keywords, but not truly a new way of thinking.

Tcl is one of a few languages (like Lisp and Erlang) that really forces you into a different way of thinking about how to design a program. Even if you don't end up using it for any real-life projects, it is still worth spending some time learning.

Further Reading and Resources

We have more guides, tutorials, and infographics related to coding and development:

What Code Should You Learn?

Confused about what programming language you should learn to code in? Check out our infographic, What Code Should You Learn? It not only discusses different aspects of the languages, it answers important questions such as, "How much money will I make programming Java for a living?"

Adam Michael Wood

About Adam Michael Wood

Adam specializes in developer documentation and tutorials. In addition to his writing here, he has authored engineering guides and other long-form technical manuals. Outside of work, Adam composes and performs liturgical music. He lives with his wife and children in California.


Thanks for your comment. It will show here once it has been approved.

Your email address will not be published. Required fields are marked *