Tcl Quick Reference
This reference collects the core Tcl syntax and command families a working developer reaches for daily: variable and command substitution, control structures, list and string manipulation, and the essential Tk widget/geometry commands, all summarized for quick lookup rather than deep explanation.
Cricket analogy: It's like a pocket-sized fielding-positions chart a young captain keeps in their pocket — not a full coaching manual, just the essential reference to glance at mid-over when setting a field quickly.
Core Syntax: Variables, Substitution, Control Flow
Variables are set with set name value and read with $name; command substitution nests a command's result into another with square brackets, set total [expr {$a + $b}]; and control flow uses if {cond} {...} elseif {cond2} {...} else {...}, while {cond} {...}, for {init} {cond} {incr} {...}, and foreach item $list {...} — each keyword is itself just an ordinary command, so if, while, and foreach all accept braces as their block arguments the same way any other command accepts a string argument.
Cricket analogy: It's like every match official — the umpire, the third umpire, the match referee — technically holding the same 'official' status under the laws of cricket, even though their duties differ; in Tcl, if, while, and foreach are all just 'commands' under the hood, no matter how special they look.
# Variables & substitution
set name "Ada"
puts "Hello, $name"
set total [expr {2 + 3}]
# Control flow
if {$total > 4} {
puts "big"
} elseif {$total == 4} {
puts "equal"
} else {
puts "small"
}
foreach item {a b c} { puts $item }
# Lists & strings
set nums [list 5 3 9 1]
set sorted [lsort -integer $nums]
set csv "a,b,c"
set fields [split $csv ","]
puts [join $fields "-"]
# Tk widgets & geometry
button .b -text "OK" -command {puts ok}
pack .b -side top -fill x
grid .b -row 0 -column 0 -sticky nsewLists, Strings, and Regular Expressions
For sequences, lappend, lindex, llength, lsort, and lsearch cover the most common needs, while split/join convert between strings and lists (split $csvLine "," then join $fields "-"). For raw text, string length, string range, string map, and string trim handle the bulk of everyday manipulation, and regular-expression work goes through regexp/regsub — for example, regexp {^\d+$} $input tests whether a string is purely numeric.
Cricket analogy: It's like a scorer's toolkit: a running tally (lappend), looking up a specific ball's outcome (lindex), counting total deliveries bowled (llength), and re-sorting a batting averages table (lsort) — each command is a distinct scorer's tool for a distinct job.
Tcl command abbreviation is allowed only when unambiguous and is a REPL convenience, not something to rely on in scripts — always write full command and subcommand names (string length, not string l) in production code for clarity and forward compatibility.
Tk Widgets and Geometry Managers
The widgets reached for most often are label, entry, button, frame, text, and canvas, each created as widgetType .path -option value ...; the three geometry managers are pack .w -side top|left|right|bottom -fill x|y|both, grid .w -row N -column N -sticky nsew, and place .w -x N -y N. Combined with bind .w <Event> {script} for interaction and wm title . / wm geometry . for window-level control, these commands cover the vast majority of everyday Tk layout work.
Cricket analogy: It's like a coach's essential kit bag: stumps, bails, a bat, gloves, and pads — a handful of core items (widgets) that, combined with a few standard drills (geometry managers), cover the vast majority of a training session's needs.
split and join are inverses only when your delimiter can't appear inside the data itself — splitting a CSV line naively on , breaks for quoted fields containing commas; for real CSV parsing, use the csv package from Tcllib instead of hand-rolled split.
- set/$ handle variable assignment and substitution; [...] performs command substitution nested inside another command.
- if/elseif/else, while, for, and foreach are ordinary Tcl commands that happen to take braced blocks as arguments.
- List commands (lappend, lindex, llength, lsort, lsearch) and split/join cover most sequence manipulation needs.
- String commands (string length, string range, string map, string trim) and regexp/regsub cover most text processing needs.
- Core Tk widgets are label, entry, button, frame, text, and canvas; each is created with widgetType .path -option value.
- The three geometry managers are pack, grid, and place, each suited to a different layout style.
- bind handles interaction and wm title/wm geometry control window-level properties.
Practice what you learned
1. What does the square-bracket syntax [...] perform in Tcl?
2. Which statement about Tcl's if, while, and foreach is correct?
3. Why can naive split $csvLine "," fail on real-world CSV data?
4. Which geometry manager positions a widget using exact x/y coordinates?
5. What does grid's -sticky nsew option control?
Was this page helpful?
You May Also Like
Tcl/Tk vs Python Tkinter
How Tcl's native Tk toolkit compares to Python's tkinter binding, and why both ultimately drive the exact same widgets.
Tcl Best Practices
Practical conventions — namespacing, structured error handling, and safe command construction — that keep Tcl scripts maintainable as they grow.
Building a Simple GUI App in Tk
A hands-on walkthrough of the widget-creation, layout, and event-handling steps needed to build a working Tk desktop app.
Tcl Interview Questions
The recurring themes behind Tcl interview questions — substitution rules, list/array/dict tradeoffs, and Tk event-loop responsiveness — and how to answer them well.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics