Navigation Logo 11.1  Starting with Tk Navigation Logo

 

 

Tk's interpreter is called wish??. The letters stand for "windowing shell" the question marks represent the version number which may not be present on your system. Figure 11.1a shows a simple example that also appears above in Interactive Execution.

When run interactively, the wish?? interpreter begins with two windows. One of these is the command window you are used to from working with Tcl. The other is the root window. The command window will show Tcl's usual command prompt. The root window will be empty. Figure 11.1a shows what the root window will look like if the script shown there is entered into the command window or executed directly.

Figure 11.1a: Putting a label in the root window.
label .example -text "Hellow Orld"
pack .example 

The label command in this script creates a widget, a potential subwindow, whose purpose is to display a label. The last two arguments in this command line declare an option that provides text for this label. The string .example names the widget. This name is returned by the label command.

The pack command arranges for the newly created label widget to be mapped to the screen as a part of the root window.

The two commands of this script could have been written this way:

pack [label .example -text {Hellow Orld}]

As you learn Tk you will probably run interactively and experiment from the command window. Later you will create scripts to execute directly so users will not need to work with a Tcl command window. Running interactively, you will want a way to trash your subwindows that does not shut down the wish?? interpreter. Simply type

destroy WIDGET_NAME
For the "Hello World" application, you would type
destroy .example

You can enter any Tcl command into the command window because Tk is essentially Tcl with some commands added to support a GUI environment. These extra commands let you set up the Tk widgets and map them to the screen.

Much of a Tk script executes because of things a user does with the widgets in the root window. The commands that you enter into the command window or execute directly merely set things up so the proper responses will be made to the user's manipulation of the widgets with keyboard and mouse.

The root window and command window are examples of windows. These are coherent rectangles on your screen that you can resize and minimize or iconize. Besides the root and command window, the other Tk windows you will deal with are menus, as described below in Menus and Menubuttons, or toplevel windows, as described below in Toplevel. Much of Tk programming involves populating windows with subwindows.

Figure 11.1b shows another initialized root window. At the top is an area where you could enter some keyboard characters. When you click the button in the middle, any letters you have typed will be converted to uppercase. Click the button at the bottom and the initialization is undone, leaving you with a raw root window.

Figure 11.1b: The caps example.

The script that creates the Caps example appears separately as Script S11.1a. Placing it in a separate script box rather than with a figure makes it available from the book's home page. Also, I prefer to attach scripts to figures when I am ready to explain them. I am not yet ready to explain S11.1a. However, to get used to Tk and acquire the right mindset for reading the next section, it would be a good idea for you to execute this script both interactively and directly.

Script S11.1a: The initialization script for the Caps example.
## Script S11.1a from "Tcl/Tk For Programmers"
proc quit {} {

global Entry

destroy .entry .edit_button .quit_button

unset Entry } entry .entry -textvariable Entry pack .entry button .edit_button -text Caps -command { set Entry [string toupper $Entry] } pack .edit_button button .quit_button -text Quit -command { quit } pack .quit_button

Remark

The batch-file method of executing tclsh?? that is shown above in Direct Execution is not necessary for wish?? – you can use the simpler method shown there.

Exercise 11.1a

Execute Script S11.1a both interactively and directly. At this point, do not worry about what is in the script – that is explained below in Details of the Caps Example.

As you experiment with the way the script runs, notice what the Tab key does. The function of the Tab key will be changed in the Enhancing Caps with Tab section.

Solution

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.