526 529




Linux Unleashed, Third Edition:Introduction to Tcl and Tk





-->















Previous
Table of Contents
Next




The button command created a button widget that we called .b. To make the button appear in the wish window, we must tell Tk to display the button. This is done by the pack command.
In this example, the pack command has only one argument: the name of the button created in the first command. When the pack command is executed, a button with the string Hello there displayed in it appears in the wish window.
Two things about this example are worth discussing in more detail. The first is why we call the button .b instead of b, bob, or button1. The significance is not the actual text in the button name (this could, in fact, be bob or button1) but the period (.) preceding the name of the button.
The period notation is used to represent the widget hierarchy. Each widget is contained in another widget. The root widget, or the highest level widget, is contained in the wish window and is called . (this is analogous to the Linux directory structure, in which each directory has an owner or a parent directory and the root or highest level directory is named /). Each time a new widget is created, we must tell Tk which widget the new widget should be contained in. In the “Hello there” example, the container specified for the button widget was ., the root widget.
The second item of interest is the resizing of the wish window that occurs after entering the pack command. The wish window shrinks to a size that is just large enough to hold the button we’ve created. Tk causes the wish window to default to a size just large enough to hold whatever it has in it. Many commands can be used to change this behavior and customize how things are displayed on the screen. You will see some of these commands later in this chapter.
The Tcl Language
Now that you have seen examples of both Tcl and Tk in action, it is appropriate to take a step back and look at the underlying Tcl language in more detail. Tcl contains a rich set of programming commands that supports all the features found in most high-level languages. This section discusses many of these features and gives examples that explain how to use them.

Tcl Variables and Variable Substitution
Like the UNIX shell programming languages, Tcl supports the concept of variables. Variables are temporary storage places used to hold information that will be needed by a program at some later point in time. In Tcl, variable names can consist of any combination of printable characters.

Typically, variable names are meaningful names that describe the information being stored in them. For example, a variable that is being used to hold the monthly sales of a product might have one of the following names:


Monthly_sales
“Monthly sales”



Note:  Quotation marks cause Tcl to ignore the whitespace characters (spaces and tabs) in the variable name and treat it as one word. This is discussed in the “Quotes” section of this chapter.

The value that is placed into a variable can also be any combination of printable characters. Possible values for the Monthly_sales variable are


“40,000”
40000
“refer to table 3”


The Tcl set command is used to assign values to variables. The set command can be passed either one or two arguments. When two arguments are passed to the set command, the first one is treated as the variable name and the second is the value to assign to that variable.
When the set command is used with only one argument, Tcl expects the argument to be the name of a variable, and the set command returns the value of that variable. The following command assigns the value of 40000 to the variable Monthlysales and then echoes the value to the screen:


set Monthlysales 40000


To print the value of the Monthlysales variable to the screen, type


set Monthlysales


All values that are assigned to variables are stored as character strings. If we define a variable to be equal to the integer 40, as in the following command



set num 40


the value 40 is represented as the character string 40, not as an integer.
So far you have seen how to set variables and how to display their values to the screen, but not how they are used with commands other than the set command. To use the value of a variable in another command, we must precede the variable name with an unquoted dollar sign ($). This tells Tcl to expect a variable name and to substitute the value of that variable for the variable name. The following example shows a simple use of variable substitution:


set Monthlysales 40000
expr $Monthlysales * 12


The first command assigns the value of 40000 to the variable Monthlysales. The expr command is used to perform mathematical evaluations of expressions. In this case it takes the value of the variable Monthlysales and multiplies it by 12.
Tcl Command Substitution
Command substitution provides a way of substituting the result of a Tcl command (or commands) into an argument of another command. The syntax for command substitution is to include the commands that are being substituted into square brackets, as follows:



set Monthlysales 40000
set Yearlyforecast [ expr Monthlysales * 12 ]


The first command once again sets the variable Monthlysales to the value of 40000. The second command makes use of command substitution to set the value of Yearlyforecast equal to the result of the command in the square braces.
In this example the substitute consists of only one command. Tcl allows the substitute to consist of any valid Tcl script, meaning that it can contain any number of Tcl commands.



Previous
Table of Contents
Next














Wyszukiwarka

Podobne podstrony:
527 529
14 (526)
526 (2)
2006 Seiko Quartz Caliber Power Consumption & Battery Guideid 526
Ch15 pg511 526
README (529)
529 530
2011 03 24 WIL Wyklad 25id 526
529 532
523 526
Readme (526)
529 (2)

więcej podobnych podstron