Chapter 2  Getting Started Writing Code
Is there an easier way to input commands?

Working from wish is okay to learn some commands and to prototype commands, but there is a better way to load commands into wish.  It is called the source command.  The command source reads a file and passes it to the current interpreter for evaluation.  This allows us to use a general text editor to write a sequence of commands.

Below is a snippet of code you can type into a text editor of your choice; then try the source command in wish.

########################################
# This is a remark or comments section.
# I used the # symbol to allow myself the
# ability to type notes for this program.
########################################
# Leave the line below out until you run source more than once.
# destroy .msg
set cow  [expr 5*7]
message .msg -text "Here is some text displayed with a substitution 5*7=$cow"
pack .msg
# End code

What should I get out of the above example?

The first thing you should get from the above example is that the # symbol is for comments and remarks called notation.  Notation is always welcomed in a program to allow the reader to understand what the programmer is trying to achieve with the given section of code and to allow for general information about the purpose of the code.  Secondly, you should notice how substitution was used to place a variable value into a string.

How many types of variables?

Tcl/Tk keeps it simple (KIS) in that there are basically 2 types of variables scalars and associative arrays.  Scalars are the basic type that hold values like numbers and strings.  Associative arrays are generally referred to as arrays and contain lists that have been indexed.  Arrays will be discussed in Chapter 6.

What is a string?

A string is a collection of characters.  Tcl is very friendly when working with strings.  There are really only 2 basic types of strings:

1.A string that allows substitution.
2.A string that does not allow substitution.

In the example we used a substitution.  You should be able to guess that the "quotation marks" denote a string that can have substitution.  The kind of string that does not allow substitution is one that is made of {curly braces}.

Assignment 2.5
Create a program with the file name ch2p5.tcl that will display the difference between the 2 types of strings.
 
 

What do you think the destroy command does?  (In detail!)
 
 

Is a number a string?

A number is composed of characters and hence can be considered a string.  However, Tcl identifies numbers and does some conversions on them.  Tcl is very friendly when working with numbers.  Tcl can identify numbers such as integers, floating point and boolean.

How precise are you?

I think this is a good time to mention that the default precision is 12 significant digits with floating point values.  Type in the following example to examine the precision issue:
##############################################
# Written by Nelson
# Example ch2p6.tcl
# Example to show the precision of Tcl by default
##############################################

# If the program has already been sourced destroy the message widgets
if {[winfo exists .msg1]} {destroy .msg1}
if {[winfo exists .msg2]} {destroy .msg2}
if {[winfo exists .msg3]} {destroy .msg3}
if {[winfo exists .msg4]} {destroy .msg4}

# Set some variable for us to work with
set x 1.23456789123
set y 1.23456789123
set output1 "x+y = [expr $x+$y]"
set output2 "x+y+11 = [expr $x+$y+11]"
set output3 "x+y+111 =  [expr $x+$y+111]"
set output4  "x+y+1111 = [expr $x+$y+1111]"

# Create the message widgets and pack them
message .msg1 -width 25c -bg red -text $output1
message .msg2 -width 25c -bg yellow -text $output2
message .msg3 -width 25c -bg green -text $output3
message .msg4 -width 25c -bg orange -text $output4
pack .msg1 .msg2 .msg3 .msg4
 

Here is a screen shot:


 

What should I get from this code?

You need to notice the way we created message widgets, used the pack command and determined if we needed to destroy any existing widgets to avoid errors.

What is your message?

A commonly used widget to display information is the message widget.  Let's take a look at some of the options that we used in the line of code:

message .msg1 -width 25c -bg red -text $output1

The widget name is .msg1, the width is 25c and the background is red.  Since the width is often important, I will explain it further.  The c in the width is for centimeters.  The full options for width are as follows:

c  centimeters
m  10s of millimeters
i  inches
p  printer's points
no tag  pixels

Does pack involve a suitcase?

Well not quite.  The pack command in this example is used to place our  message  widgets in our display window.  Hence, the pack command is for placing widgets.

Assignment 2.6
Type in the example code 2.6 then try the following:

Change the line that reads:
 pack .msg1 .msg2 .msg3 .msg4
To:
 pack .msg2 .msg4 .msg1 .msg3

Change the lines that read:
 message .msg1 -width 25c -bg red -text $output1
 message .msg2 -width 25c -bg yellow -text $output2
To:
 message .msg1  -bg red -text $output1
 message .msg2  -bg yellow -text $output2

What can you now conclude about the pack command after these changes?
 
 

What can you conclude about the width option?
 
 
 

These changes will then make the example look something like this:

##################################################
# Written by Nelson
# Example ch2p6b.tcl
# Example to show the precision of Tcl by default
##################################################

# If the program has already been sourced destroy the widget
if {[winfo exists .msg1]} {destroy .msg1}
if {[winfo exists .msg2]} {destroy .msg2}
if {[winfo exists .msg3]} {destroy .msg3}
if {[winfo exists .msg4]} {destroy .msg4}

set x 1.23456789123
set y 1.23456789123
set output1 "x+y = [expr $x+$y]"
set output2 "x+y+11 = [expr $x+$y+11]"
set output3 "x+y+111 =  [expr $x+$y+111]"
set output4  "x+y+1111 = [expr $x+$y+1111]"
message .msg1  -bg red -text $output1
message .msg2  -bg yellow -text $output2
message .msg3 -width 25c -bg green -text $output3
message .msg4 -width 25c -bg orange -text $output4
pack .msg2 .msg4 .msg1 .msg3

With the output looking something like this:



Next Section

Table of Contents