Chapter 52. Tcl 7.5/Tk 4.1

Tk 4.1 is notable for its cross-platform support. Your Tk scripts can run on Windows, Macintosh, and UNIX. The associated Tcl release, 7.5, saw significant changes in event-driven I/O, network sockets, and multiple interpreters.

Cross-platform support, network sockets, multiple Tcl interpreters, and an enhanced foreach command are the highlights of Tcl 7.5 and Tk 4.1.

Cross-Platform Scripts

Cross-platform support lets a Tcl/Tk script run unchanged on UNIX, Windows, and Macintosh. However, you can still have platform dependencies in your program. The most obvious dependency is if your script executes other programs or uses C-level extensions. These need to be ported for your script to continue to work.

File Name Manipulation

File naming conventions vary across platforms. New file operations were added to help you manipulate file names in a platform-independent manner. These are the file join, file split, and file pathtype operations, which are described on page 110. Additional commands to copy, delete, and rename files were added in Tcl 7.6

Newline Translations

Windows and Macintosh have different conventions for representing the end of line in files. These differences are handled automatically by the new I/O subsystem. However, you can use the new fconfigure command described on page 231 to control the translations.

The tcl_platform Variable

In practice you may need a small amount of platform-specific code. The tcl_platform array holds information about the computer and operating system that your script is running on. This array is described on page 193. You can use a script file with the name of the platform to isolate all your platform-specific code. The following command sources either unix.tcl, windows.tcl, or macintosh.tcl from your script library:

source [file join $lib $tcl_platform(platform).tcl]

The console Command

The Windows and Macintosh versions of wish have a built-in console. The commands you enter in the console are evaluated in the main Tcl interpreter, but the console is really implemented in another Tcl interpreter to avoid conflicts. You can show and hide the console with the console command, which is described on page 29.

The clock Command

The clock command eliminates the need to exec date to get the time of day in Tcl. The equivalent is:

clock format [clock seconds]

The format operation takes an optional format string that lets you control the date and time string. There is also clock scan to parse clock values, and clock clicks to get high resolution clock values. The clock command is described on page 183.

The load Command

The load command supports shared libraries (i.e., DLLs) that implement new Tcl commands in compiled code. With this feature, the preferred way to package extensions is as a shared library. This eliminates the need to compile custom versions of wish if you use extensions. The details about creating shared libraries are described on page 697. For example, you could load the Tix library with:

load libtix.so Tix

The info command added two related operations, sharedlibextention and nameofexecutable, which are described on page 192.

The package Command

The package command provides an alternate way to organize script libraries. It also supports extensions that are added with the load command. The package command supports a provide/require model where packages are provided by scripts in a library, and your application specifies what it needs with package require commands. The package facility supports multiple versions of a package, if necessary. Packages are described on page 173.

Multiple foreach loop variables

This is one of my favorite features. The foreach command supports multiple loop variables and multiple value lists. This means that you can assign values to multiple variables during each loop iteration. The values can come from the same list or from lists that are processed in parallel. Multiple foreach loop variables are described on page 81. For example, you can iterate through the contents of an array with:

foreach {name value} [array get arrName] {
    # arrName($name) is $value
}

Event Loop Moves from Tk to Tcl

To support network sockets, the event loop was moved from Tk to Tcl. This means that the after and update commands are now part of Tcl. The fileevent command was added to support nonblocking I/O. The vwait command was added to Tcl and is equivalent to the tkwait variable command. Event-driven I/O is described in Chapter 16 starting on page 227.

The tkerror command has been replaced by bgerror. This is the procedure that is called when an error occurs while processing an event. Backwards compatibility is provided if you already define tkerror. These procedures are described on page 202.

Network Sockets

The socket command provides access to TCP/IP sockets. There are C APIs to define new channels, and there are extensions that provide UDP and other protocols. Chapter 17 describes sockets starting on page 239. Example 43-4 on page 653 uses sockets as a replacement for the Tk send command.

info hostname

The info hostname command was added to find out your host identifier.

The fconfigure Command

The best way to use sockets is with event-driven I/O. The fileevent command provides part of the solution. You also need to be able to control the blocking behavior and buffering modes of sockets. The fconfigure command lets you do this and more. You can also control the newline translation modes and query socket-specific settings. The fconfigure command is described on page 231.

Multiple Interpreters and Safe-Tcl

Chapter 19 describes the new interp command and the Safe-Tcl security mechanism. You can create multiple Tcl interpreters in your application and control them with the interp command. You create command aliases so that the interpreters can exchange Tcl commands. If an interpreter is created in a safe mode, then its set of Tcl commands is restricted so that its scripts cannot harm your computer or application. However, with aliases you can give the untrusted scripts limited access to resources.

The grid Geometry Manager

Chapter 26 describes the new grid geometry manager that provides a table-like metaphor for arranging widgets. Like pack, grid is constraint-based, so the grid automatically adjusts if widgets change size or if widgets are added and deleted. The grid command was influenced by the blt_table geometry manager, but it is a whole new implementation.

The Text Widget

Several new operations were added to the text widget. The dump operation provides a way to get all the information out of the widget, including information about tags, marks, and embedded windows. The mark next and mark previous operations let you search for marks. The tag prevrange is the complement of the existing tag nextrange operation.

The Entry Widget

The bbox operation was added to the entry widget. This is used to refine the bindings that do character selection and set the input cursor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.198.43