Tcl and Tk

The Tcl and Tk toolkit were written by John Osterhout to provide a way of putting together command languages. Tk builds X Windows interfaces in a simple way. It provides widgets and a method for packing them together. This is a very quick look at the system a more detailed overview of the syntax is available.

Here is an example of a Tk script that reads a file into a text widget. It is copied from Osterhout's book except that I put the scrollbar on the left.


# text: read a file into a text widget.



text .text -relief raised -bd 2 \

         -yscrollcommand ".scroll set"

scrollbar  .scroll  -command ".text yview"

pack .scroll -side left -fill y

pack .text -side right



proc loadFile file  {

      .text delete 1.0 end

      set f [open $file]

      while {! [eof $f] } {

              .text insert end [read $f 1000]

       }

      close $f

}

 

loadFile  README

"." is the toplevel widget. So that .text and .scroll are beneath "." in the hierarchy. Lower level items have further . separators in them thus

.menu.help
would refer to an entry in a menu.
.dlg.msg
would be a message in a dialog box etc.

To use Tcl scripts begin them with the first line

#!/macslocal/bin/wish -f
This will work when I get the code moved there.


Return to GUI home page

Last Changed: 22 June 1995