Next
Contents 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language

A Tcl(Tutorial for Cool Languages) for Tcl/Tk

Tcl/Tk Logo

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

Next
Contents 

Comments

Mariusz at 18 Mar, 2007 03:23
I have lerned a lot from here, Thanks
Reply to this.
Ari at 17 Apr, 2007 08:05
Dear Sir,
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.
Reply to this.
Binny V A at 17 Apr, 2007 11:45
I am sorry - but I have not yet heard of that application. I could not help you out here.
Reply to this.
tamgo at 16 May, 2007 06:03
Excellent resource! Thanks, this helped a lot.
Reply to this.
Souaveck at 19 May, 2007 11:46
Hey Guys! Great page!!
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
Reply to this.
Anonymous at 22 May, 2007 02:09
This document is awesome!!!
Any clues on how do display x-y plots?
Reply to this.
Apud at 05 Jul, 2007 11:53
I would suggest using BLT for plots.

blt::graph works very nicely and you can also generate PS or print out the plot.
Reply to this.
himanshu pandey at 25 May, 2007 05:15
Very good tutorial for beginners
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
Reply to this.
Abhishek Kumar at 13 Jul, 2007 04:58
A great great tutorial. it was fun going through it and trying out the programs myself.
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?
Reply to this.
Anonymous at 02 Apr, 2008 10:44
We need to bind a handler (a procedure) to the <Resize> Event. In the handler you can re-create all the elements inside the window based on the size of the newly-resized window.

However, I am not sure if TCL defines such an event.
Reply to this.
Fruttenboel at 21 Jul, 2007 03:30
While looking for something else I ran into an example of TCL/TK and I liked it.
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
Reply to this.
Anonymous at 04 Aug, 2007 02:13
Your Tcl/Tk tutorial is the best I could find on google!
Thanks!
Please keep writing and add more things to your tutorials!
Reply to this.
Anonymous at 30 Aug, 2007 02:42
hello sir,

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
Reply to this.
Binny V A at 30 Aug, 2007 06:09
Try 'wish FILE.tcl'
Reply to this.
The New Mr at 19 Sep, 2007 05:22
Excellent little tutorial man! Learnt a lot from it in a short space of time. There are a few grammatical mistakes here and there but you make up for it with your humor spread out over the tutorial. Loved the bit about the pizza :)
Reply to this.
Binny V A at 17 Jan, 2008 12:09
Thanks :-)
Reply to this.
Mr x at 19 Sep, 2007 07:12
HI,
HOW to run another command using TK.
like i want to run another application when a button is clicked.
Reply to this.
Binny V A at 19 Sep, 2007 11:34
Use the exec function.
Reply to this.
Anonymous at 05 Oct, 2007 05:44
Hi can anyone tell me how to write a tcl proc where I can get admits from all the days till they become discharges and it should be displayed every day.
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
Reply to this.
Tcl newbie at 06 Oct, 2007 03:27
Hi all,

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

Reply to this.
Anonymous at 26 Dec, 2007 09:56
dear sir,
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
Reply to this.
Anonymous at 22 Jan, 2008 02:23
sir
can u give me the coding for database connectivity using tcl script to sqlserver 2005
Reply to this.
Anonymous at 23 Jan, 2008 11:48
Hello Sir/Madam,

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 ..
Reply to this.
Anonymous at 24 Jan, 2008 01:05
i have loaded the required dlls but it give the error message saying "cannot connect to localhost <10061> . can any one suggest me a solution..
Reply to this.
Anonymous at 23 Mar, 2008 08:50
HI! i require a VHDL code for creating a GUI screen with one or two radio buttons. I think it can be done using TK. Can u please gimme a code for the same? its pretty urgent.
Reply to this.
Anonymous at 27 Mar, 2008 04:01
HI! I need a code for creating a GUI screen for data entry. There are two input data to be obtained from the user. I am using Modelsim SE 6.0 version. This data entry screen is part of my project. I am unable to write the code for it. If not the code, atleast gimme guidelines abt using tcl/tk in modelsim. r gimme a simple program that i can execute using modelsim so that i understands how tcl/tk is used in modelsim.
Reply to this.
Anonymous at 07 Apr, 2008 07:40
Hi Binny,

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.
Reply to this.
Sudoku Maniac at 07 Apr, 2008 04:52
does it work on mobiles ?
Reply to this.
Brinda at 01 May, 2008 03:58

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
Reply to this.
Anonymous at 08 May, 2008 11:21
Sir,

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
Reply to this.
Anonymous at 15 May, 2009 04:53
script is fine .Be careful with spaces while writing
set recList[ lsort -unique $recList] will not work
set recList [ lsort -unique $recList] will work.

Thanks
Reply to this.
Anonymous at 20 May, 2008 04:23
Hi,
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"

Reply to this.
Kurnal Madhyan at 11 Jul, 2008 04:37
Hi,

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
Reply to this.
Anonymous at 20 Sep, 2008 07:32
hi, Can anyone tell me how to send radiobutton -command as list so that i can 2 procedure in that and proceed.

Regards
Sreeram
Reply to this.
Arunanshu at 01 Oct, 2008 12:53

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)
Reply to this.
Binny V A at 01 Oct, 2008 05:39
Try it without the {} around $stream.
Reply to this.
Arunanshu at 01 Oct, 2008 11:03
First of all thanks for the fastest reply.

I still get the SAME ERROR.
Reply to this.
Arunanshu at 01 Oct, 2008 11:29
I got it.
This line(#) should be written as follows(-->):
# switch { $stream }
# {

--> switch $stream {
Hope I am correct. :-)
Thanx
Binny.
Reply to this.
Bidyut at 21 Nov, 2008 03:47
set stream [lindex $argv 1]
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
Reply to this.
Anonymous at 22 Nov, 2008 02:15
Yes, I worked it out ling back.
;-)

thanx man.
cheers!!
Arunanshu
Reply to this.
AJ at 22 Jan, 2009 03:57
You have written the best tutorial ever found on Web.
You tutorial have made TCL/TK as easy as that anyone can grasp it in 1-day!
Thanks a lot. Please keep writing.

Reply to this.
Anonymous at 10 Feb, 2009 06:19
Hi All,
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
Reply to this.
Anonymous at 27 Apr, 2009 03:24
I am not able to see the console window when i am running a batch file through TCL.
Can any one help me to solve this problem
Reply to this.
zubair at 05 Jun, 2009 04:21
I am using Tcl in ReplayXcessory for automated testing of Motif applications and I found this tutorial to be great

Regards,
zubair



Reply to this.
arindam at 30 Nov, 2009 04:43
hello, i read ur tutorial,it was really nice.
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..
Reply to this.
David at 15 Apr, 2010 01:59
I am using tcl from C++. Can i know how to get the error that is return by tcl? I tried to use Tcl_GetErrno() which always return the error message saids that "no such file" even though there are no errors occur.

Can you please provide some advice?
Thanks
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed