simple Tk example

Bla Bla This simple example was built by me as a tutorial to sort out how to do basic Tk. The books I reference in the text have supplied most of the material. Neither gathered at one place how to put together a set of frames with a callback to actually do something. The other tricky bit is how to pick up the new filename from the entry where it is input and pass it to the file reading section. This runs fine with the latest release of Tclkit on a Windows 98 machine. I have not tried it anywhere else. John Fletcher June 2000.

Note: Tcl comments need to start at the beginning of the line. So in this system they all run on as text. They will be O.K. if you cut and paste this to your own editor. If you edit this page it will look as it should.

Filename: texttk.tcl

# texttk: read a file into a text widget # and read another one if the name is changed. # It reports errors # This is based on example material in # "Tcl and the Tk Toolkit" by Ousterhout p.216 # with some more stuff added from # "Tcl/Tk in a Nutshell" by Raines and Tranter

   set w .
   set m [menu $w.menubar -tearoff 0]
   $m add cascade -label File -menu [menu $m.file]
   $m.file add command -label Quit -command exit
   $m add cascade -label Help -menu [menu $m.help]

# The command has to be in braces, not square brackets. # If otherwise it is obeyed on loading and not when the menu is used.

   $m.help add command -label Index -command {puts Sorry}
   $w configure -menu $m

# The first frame has the entry to give the file name.

   set f [frame $w.f1]
   pack [label $f.label -text {Enter File name}] -side left
   pack [entry $f.entry -textvariable fileToOpen] -side left -fill x -expand true
   $f.entry insert 0 texttk.tcl
   pack $f -fill x -padx 3 -pady 2

# The next name reports the file name currently open

   set f1 [frame $w.f11]
   label $f1.label -text "Filename is:"
   label $f1.filename -textvariable fileOpened
   pack $f1.label $f1.filename -side left
   pack $f1 -fill x -padx 3 -pady 2

# This frame holds the actual text

   set f2 [frame $w.f2]
   pack [label $f2.filename -textvariable $fileToOpen] -side left -fill x -expand true
   text $f2.text -relief raised -bd 2 -yscrollcommand "$f2.scroll set"
   scrollbar $f2.scroll -command "$f2.text yview"
   pack $f2.scroll -side right -fill y
   pack $f2.text -side left
   pack $f2 -fill x -padx 3 -pady 2

# This loads the file #It does not work

   proc loadFile { } {
      global f
      global f2
      global fileToOpen
      global fileOpened
      if [file exists $fileToOpen] {
         $f2.text delete 1.0 end
         set ff [open $fileToOpen]
         while {![eof $ff]} {
            $f2.text insert end [read $ff 1000]
         }
         close $ff
         set fileOpened $fileToOpen
      } else {
         $f.entry insert end " does not exist"
      }
   }

# This enables the button to load on demand

   button $f.b -text "Load File" -command loadFile
   pack $f.b

# This enables load when return is pressed in the entry field

   bind $f.entry <Return> loadFile

# This does the first load

   loadFile

# End of example.

Usage: Either type

     tclkit texttk.tcl

which will load the file straight away or type

     tclkit

and at the prompt:

                     source texttk.tcl

At the moment the help index just does a puts to the console. I have a better version which puts up a message box which can then be cancelled. It took a certain amount of fiddling with the difference between braces, square brackets and quotes. I am sure experienced Tk'ers know all this. It is just that simple examples seem not to be very accessible to me. It is all part of the learning process!