Wiki source for ToolCommandLanguage


Show raw source

{{include tonguesToolCommandLanguage}}
[[HomePage]] > [[ComponentHowTo Components and HowTos]] > [[HowToProgramming Programming]]

====Programming in Tcl/Tk====
This is a page for people interested in learning how to program using tcl and Tk.
---
""
Lets get straight into <strong>Tool Command Language </strong>( tcl ).<br />
If you are in Puppy click on rxvt and type in<br />
<br />
<div class="indent">
wish
</div>
<br />
you should have a window open on the desktop named <strong>wish</strong> and the # prompt should have changed to a %.<br />
<br />
Now for the classic ' Hello World! ' program, type<br />
<br />
<div class="indent">
puts "Hello World!" and hit enter
</div>
<br />
Hello World! should appear at the prompt.<br />
<br />
Congratulations, you have just written your first tcl program. <br />
<br />
To dissect this lets look at what happened. <br />
First you opened the tcl wish shell and then entered a command ' puts'.<br />
This prints to the standard output which is the terminal that you are working in. <br />
What it outputs is the text enclosed by the " " . <br />
<br />
Calculations are done with the command <strong>expr</strong>:<br />
Type:<br />
<div class="indent">
expr 1+2
</div>
and Tcl will respond with "3".<br />
<br />
To force calculation in floating-point, add a decimal point:<br />
<div class="indent">
expr 17/4.0
</div>
<br />
Assign and use a variable:
<div class="code">
<pre>
set x 17
puts "x=$x"
</pre>
</div>
<br />
<br />
<div class="code">
<pre>
set t1 Hello
set t2 "Good morning"
puts "$t1, $t2"
</pre>
</div>
<br />
<br />
A complete, simple example:<br />
<div class="code">
<pre>
set x 17
set y 4.0
set z [expr $x/$y]
puts "$x/$y=$z"
</pre>
</div>
<br />
The square brackets above are used to group the result that gets assigned to z.<br />
<br />
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br />
<br />
<div class="indent">
<strong>tcl/Tk more info</strong>
</div>
<br />
Tcl is an interpreted language which means that you do not have to run it through a compiler to create a program, <br />
you can write a tcl program and run it to see the results immediately.<br />
Tcl is composed of built in commands and commands you can create yourself called procedures <br />
which use the name <strong> proc</strong> and are what you could call <strong>sub-programs</strong> or <strong>macros</strong>.<br />
<br />
Here is an example of a small procedure that calculates the square of its argument,<br />
and a loop to output a simple table:<br />
<div class="code">
<pre>
proc square {x} { return [expr $x*$x] }
for {set x 1} {$x <= 5} {incr x} {
set y [square $x]
puts "$x $y"
}
</pre>
</div>
<br />
<br />
Tk is the <strong>Tool kit</strong> used by tcl and is used to create "windows" to view and display information and graphics. <br />
The main window that you create is represented by a period <strong>.</strong> and any frames or widgets created <br />
inside that window start with a <strong>.</strong> so if a frame is created to contain text <br />
you could name it something like <strong>.txt</strong> and any text entered would be referenced to <strong>.txt</strong> as in:<br />
<div class="code">
<pre>
package require Tk
text .txt
pack .txt
.txt insert end "Hello World!"
</pre>
</div>
<br />
which means define a text-frame named ".txt", use the <strong>pack</strong>er to put it into the window <strong>.</strong>, <br />
then insert the strings "Hello World!" at the end of the text frame.<br />
<br />
To make this Tk-example somewhat interactive, we can add two buttons to the above code:<br />
<div class="code">
<pre>
button .but1 -text "Bla" -command {.txt insert end "Bla\n"}
button .but2 -text "Quit" -command {exit}
pack .but1 .but2
</pre>
</div>
<br />
<br />
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br />
Please have a look here for more, <a href="http://puppylinux.org/wikka/LearnTclTk">LearnTclTk</a><br />
<br />
<hr />
<img title="WikiImage" alt="image" src="http://images.google.com/images?q=tbn:nUnkumtcq_MJ:http://www.nlgsys.net/graf/allas.gif" class="center" /> <br />
<br />
<a href="http://www.dci.clrc.ac.uk/Publications/Cookbook/" class="ext">tcl cookbook</a><span class="exttail">∞</span><br />
About <a href="http://puppylinux.org/wikka/TclTk">TclTk</a>, there is a highly recommended TclTutor, a very good starting point:<br />
<br />
<a href="http://www.msen.com/~clif/TclTutor.html" class="ext">http://www.msen.com/~clif/TclTutor.html</a><span class="exttail">∞</span> <br />
<a href="http://wiki.tcl.tk/" class="ext">http://wiki.tcl.tk/</a><span class="exttail">∞</span><br />
<br />
Uses the tcl/tk to present tcl/tk source code learning, it is really really really worth the effort to download and install if you have any interest in understanding or using tcl/tk.<br />
<br />
Here is a more up to date tutorial covering latest features:<br />
<a href="http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html" class="ext">http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html</a><span class="exttail">∞</span><br />
<br />
Other Tcl/Tk Resources<br />
<a href="http://www.devsource.com/article2/0,1895,1778148,00.asp" class="ext">Tcl/Tk Surprise * 5</a><span class="exttail">∞</span><br />
<a href="http://users.belgacom.net/bruno.champagne/tcl.html" class="ext">tcl tutorial</a><span class="exttail">∞</span><br />
<a href="http://wiki.tcl.tk/1304" class="ext">wiki.tcl.tk Tclers' Wiki has a page devoted to online tutorials </a><span class="exttail">∞</span><br />
<a href="http://groups-beta.google.com/group/comp.lang.tcl/browse_thread/thread/35c4a2cd3e4d1b81/" class="ext">comp.lang.tcl </a><span class="exttail">∞</span><br />
<a href="http://wiki.tcl.tk/9058" class="ext">Teach Programming to Children</a><span class="exttail">∞</span><br />
<br />
Tcl Manual in other Languages, Dutch, Finnish, French, German .... (11 different languages)<br />
<a href="http://wiki.tcl.tk/3205" class="ext">Endekalogue</a><span class="exttail">∞</span><br />
<br />
<a href="http://tiger.la.asu.edu/quick_reference_card.htm" class="ext">Reference card</a><span class="exttail">∞</span> - also for C and Bash<br />""

==Also on the Wiki==
~[[TinyCc]] - tiny C compiler

==Related Webpages==
~http://www.tcl.tk/

----
==Categories==
CategoryDevelopment
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki