Contents
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
A Tcl(Tutorial for Cool Languages) for Tcl/Tk
Tcl/Tk - one language, many uses. It can be used for GUI(Graphical User Interface) development with the powerful TK. Tcl/Tk runs as a plugin from web browsers made by Netscape and Microsoft. So it can be used instead of JavaScript and VBScript. It is a CGI language and can be used in servers. Tcl-Tk is also extremely extendable - Tcl/Tk is valuable to C/C++ programmers who want to learn a high-level scripting language for their user interfaces or for integrating pieces of a large system.
Tcl stands for Tool Command Language. Tcl and Tk, its associated graphical user interface toolkit, was created by Professor John Ousterhout of the University of California, Berkeley. Tcl is a scripting language that runs on Windows, UNIX and Macintosh platforms. Tk is a standard add-on to Tcl that provides commands to quickly and easily create user interfaces. Even though Tcl was originally created on UNIX, your Tcl/Tk scripts should run the same on all supported platforms, except for a few differences.
Tcl is a very simple, open-source-licensed programming language and provides basic language features such as variables, procedures, and control, and it runs on almost any modern OS, such as Unix, Macintosh, and Windows 95/98/NT/XP computers.
Tcl was originally developed as a reusable command language for experimental computer aided design(CAD) tools. The interpreter was implemented as a C library that could be linked into any application. It is very easy to add new functions to the Tcl interpreter, so it is an ideal reusable "macro language" that can be integrated into many applications.
But Tcl is a programming language in its own right, which can be roughly described as a cross-breed between LISP/Scheme (with less parens) and shells (with more powerful structuring). You can write any number of programs in Tcl, just as you can in any other language. Tcl programs are usually called "scripts" because the programs do not need to be compiled into a machine-readable form.
Enough history(..yawn..). Now lets move on to the tutorial. Go to the contents page - or skip straight to the important parts.
Author - Binny V A
About the Author
Personal Site Binny V A
Tutorial Feedback

Comments
My Name is Ari.I am From Indonesia,I am interesting with your experience.Now i am making paper about softswitch simulation using NS2(TCL).
And i feel confused about the scripts of it.Would you please do me a favor?Thank you for your attention.
The one thing which would be very usefull is a screeshot of the results at the end of each tutorial. It makes easier to find something what beginners (as me) are looking for.
Thanks a lot!! Great job. I can say that this page is definatelly "zajebista" :-)))
Regards
Souaveck
Any clues on how do display x-y plots?
blt::graph works very nicely and you can also generate PS or print out the plot.
However one more piece of information could be added like how do run scripts
like the script requires packages like BWidget else it gives Err: invalid command
But overall I would give 8.5 out of 10 to the contents and presentations style
Thank you very much. you have given me a solid base in tcl\tk and now I can go further.
I have one problem, the widgets packed through grid or pack commands don't resize themselves when the main window is resized. Can u please tell me how to do that?
And why don't you give some more information about yourself on your site?
However, I am not sure if TCL defines such an event.
Looking for a nice tutorial I ran into your site and I like it as well.
You are a gifted writer.
I want TCL/TK on the side of Mocka for controlling my Parilux feedback device:
verhoeven272.nl/fruttenboel/parino/palux1.html
Thanks!
Please keep writing and add more things to your tutorials!
i want to know,how to get the output on a command prompt.that is like java,we give the command javac filename ---> java file...we will get output in the command prompt...similarly we write the tcl/tk code in wordpad(.tcl) and how can we get the output on command prompt
HOW to run another command using TK.
like i want to run another application when a button is clicked.
ex:Like say Charlie got admitted on 10/01 and discharged on 10/4
i want to see him on the frl every day while I ftp
10/01 charlie
10/2 charlie
10/3 charlie
104/ charlie as discharge
I am new to tcl i have a tcl script which has mysqltcl package. I want to use this as tclet i,e view this tcl script inside browser. i downloaded plugins for that. But browser does't get the mysqltcl package. But it runs fine when run from wish. any suggestion will be much appreciated
i m new tcl/tk i want to learn it plz tell me hw can i proceed in tcl/tk , i dnt knw any scripting langauge
can u give me the coding for database connectivity using tcl script to sqlserver 2005
I am unable to find the "LIBMYSQLTCL.SO" file . Can anyone suggest me where can i get the exact file . Please provide me a solution ..
Thanx a lot for your great work on this site & i am a VLSI design engineer (IC design), i was not having any knowledge of scripting languages like TCL/Tk i got your site and downloaded it and trying to learn it properly, but my main intention of learning is EDA (Electronics Design Automation) using TCL/TK. Though it works well for text file operations/data processing. But, could you be able to give me a clue or some way that would automate the geomatry based placement & routing of layers in IC layouts like eg. metal layers, schematic circuit symbols in library (of a tool env. - i am hav ing this thing but only need a procedure to route them) & pcells (parametric cells designed and characterized for multiple climatic & surrounding parameters) just i wanted to know how to move the polygons (cells/physical graphical instances/polygons like standard cells/basic building blocks in IC layout editor (like Cadence - Virtuoso XL/LE or Magma Blast fusion or IC compiler...etc)), may be it's coordinate based or grid count based in the design editors. i have also tried running examples of GUI development to get a clue of generating some grraphical symbols/may be it would give an idea of P&R of instances for buttons or entry boxs, may know how to execute these commands is there some thing special library or functions to be loaded in some location to call and run these commands. I wanted it for home usage for learning in Windows env. would these command be able to run in this OS or could only be ran on linux/solaris/unix? Would really grateful if you can give a funda/direction of in which i should think for doing moving & propagating some layers having specific dimentional ans spacing constraints.
Thanks & Regards,
Ravi.
Dear Sir,
Using TCl/TK I have created a GUI,now i want to store and retrieve data using the same language tcl/tk ..,could u please help me in that sir.,I am helpless.
Regards,
Brinda.S
I would like to know how to eliminate duplicates from a list object using TCL code
I tried this:
set recList [list]
lappend recList "two"
lappend recList "one"
lappend recList "two"
lappend recList "five"
set recList [lsort -unique $recList]
I get an error when I use -unique syntax. Please help me with this by giving a sample code.
Thanks,
Kamesh
set recList[ lsort -unique $recList] will not work
set recList [ lsort -unique $recList] will work.
Thanks
I am making one script in which i want to create a child console from linux.
Below is the script. Can anybody make me understand what and how this script is working.
Your cooperation will give me great help to do some good practise.
if {![catch {console show}]} {
return
}
if {[catch {package require Tcl 8}]} {
package require Tcl 7.5
}
if {[catch {package require Tk 8}]} {
if {[catch {package require Tk 4.1}]} {
return -code error "Tk required but not loaded."
}
}
set _ [file join $tk_library console.tcl]
if {![file readable $_]} {
return -code error "File not readable: $_"
}
set consoleInterp [interp create]
$consoleInterp eval [list set tk_library $tk_library]
$consoleInterp alias exit exit
load "" Tk $consoleInterp
# 2. A command 'console' in the application interpreter
;proc console {sub {optarg {}}} [subst -nocommands {
switch -exact -- \$sub {
title {
$consoleInterp eval wm title . [list \$optarg]
}
hide {
$consoleInterp eval wm withdraw .
}
show {
$consoleInterp eval wm deiconify .
}
eval {
$consoleInterp eval \$optarg
}
default {
error "bad option \\\"\$sub\\\": should be hide, show, or title"
}
}
}]
;proc consoleinterp {sub cmd} {
switch -exact -- $sub {
eval {
uplevel #0 $cmd
}
record {
history add $cmd
catch {uplevel #0 $cmd} retval
return $retval
}
default {
error "bad option \"$sub\": should be eval or record"
}
}
}
if {[package vsatisfies [package provide Tk] 4]} {
$consoleInterp alias interp consoleinterp
} else {
$consoleInterp alias consoleinterp consoleinterp
}
bind . <Destroy> [list +if {[string match . %W]} [list catch \
[list $consoleInterp eval tkConsoleExit]]]
rename puts tcl_puts
;proc puts {args} [subst -nocommands {
switch -exact -- [llength \$args] {
1 {
if {[string match -nonewline \$args]} {
if {[catch {uplevel 1 [linsert \$args 0 tcl_puts]} msg]} {
regsub -all tcl_puts \$msg puts msg
return -code error \$msg
}
} else {
$consoleInterp eval [list tkConsoleOutput stdout \
"[lindex \$args 0]\n"]
}
}
2 {
if {[string match -nonewline [lindex \$args 0]]} {
$consoleInterp eval [list tkConsoleOutput stdout \
[lindex \$args 1]]
} elseif {[string match stdout [lindex \$args 0]]} {
$consoleInterp eval [list tkConsoleOutput stdout \
"[lindex \$args 1]\n"]
} elseif {[string match stderr [lindex \$args 0]]} {
$consoleInterp eval [list tkConsoleOutput stderr \
"[lindex \$args 1]\n"]
} else {
if {[catch {uplevel 1 [linsert \$args 0 tcl_puts]} msg]} {
regsub -all tcl_puts \$msg puts msg
return -code error \$msg
}
}
}
3 {
if {![string match -nonewline [lindex \$args 0]]} {
if {[catch {uplevel 1 [linsert \$args 0 tcl_puts]} msg]} {
regsub -all tcl_puts \$msg puts msg
return -code error \$msg
}
} elseif {[string match stdout [lindex \$args 1]]} {
$consoleInterp eval [list tkConsoleOutput stdout \
[lindex \$args 2]]
} elseif {[string match stderr [lindex \$args 1]]} {
$consoleInterp eval [list tkConsoleOutput stderr \
[lindex \$args 2]]
} else {
if {[catch {uplevel 1 [linsert \$args 0 tcl_puts]} msg]} {
regsub -all tcl_puts \$msg puts msg
return -code error \$msg
}
}
}
default {
if {[catch {uplevel 1 [linsert \$args 0 tcl_puts]} msg]} {
regsub -all tcl_puts \$msg puts msg
return -code error \$msg
}
}
}
}]
$consoleInterp alias puts puts
set tcl_interactive 1
$consoleInterp eval source [list [file join $tk_library console.tcl]]
$consoleInterp eval {
if {![llength [info commands tkConsoleExit]]} {
tk::unsupported::ExposePrivateCommand tkConsoleExit
}
}
$consoleInterp eval {
if {![llength [info commands tkConsoleOutput]]} {
tk::unsupported::ExposePrivateCommand tkConsoleOutput
}
}
if {[string match 8.3.4 $tk_patchLevel]} {
# Workaround bug in first draft of the tkcon enhancments
$consoleInterp eval {
bind Console <Control-Key-v> {}
}
}
# Restore normal [puts] if console widget goes away...
proc Oc_RestorePuts {slave} {
rename puts {}
rename tcl_puts puts
interp delete $slave
}
$consoleInterp alias Oc_RestorePuts Oc_RestorePuts $consoleInterp
$consoleInterp eval {
bind Console <Destroy> +Oc_RestorePuts
}
unset consoleInterp
console title "[wm title .] Console"
I have been developing an application in Perl/TK.I want to get rid of command prompt coming with it.
Please suggest me ways to remove the command prompt
Thanks and Regards,
Kurnal Madhyan
Regards
Sreeram
Just a beginnner. Kindly have a look at this snippet and inform me of the correct solution. Working on TCL-8.4
set stream [lindex $argv 1]
switch { $stream }
{
"IS"{
#Internet Stream
puts "JAVA"
}
"OS"{
#Open System
puts "LINUX"
}
"MF"{
#Main Frame
puts "COBOL"
}
"default"{
#No Project
puts "B.E.N.C.H."
}
}
puts ""
When on executing this program using the following line "tclsh practice.tcl Arunanshu OS" to run the program I expect this output:
LINUX
Whereas on execution, I get this output:
wrong # args: should be "switch ?switches? string pattern body ... ?default body?"
while executing
"switch { $stream }"
(file "array.tcl" line 101)
I still get the SAME ERROR.
This line(#) should be written as follows(-->):
# switch { $stream }
# {
--> switch $stream {
Hope I am correct. :-)
Thanx
Binny.
switch $stream {
"IS" {puts "JAVA"}
"OS" {puts "LINUX"}
"MF" {puts "COBOL"}
"default" {puts "B.E.N.C.H."}
}
puts ""
this is the correct code... I have tried and confirmed it...
Rply plz if you are satisfied
;-)
thanx man.
cheers!!
Arunanshu
You tutorial have made TCL/TK as easy as that anyone can grasp it in 1-day!
Thanks a lot. Please keep writing.
I'm just a beginer in tcl/tk programming...I'm trying to create a simple messagebox using TK in RobCAD application. But I'm not being able to do it...can anyone help??
Thanks & regards,
Subhrajit
Can any one help me to solve this problem
Regards,
zubair
Well, i want to make an IDE in C and using TCL/TK for GUI. I searched for tutorials on 'embedding tcl in c', but am not able to understand how to make an API for it?? can u please help me out..
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.