Prev - Index

A tcl/tk example

This example uses Tk to create a graphical text file viewer, specified on the command line.

It could be easily extended to accept multiple files or input from stdin.

#!/usr/bin/env wish
# A Tk fileviewer - doesn't handle errors very gracefully!
# A simple demonstration of Tcl/Tk for the March 1999 LUV talk
# Written by Graeme Cross

if {$argc != 1} {
   puts "\nError - Syntax is: tkmore filename\n"
   exit 1

set viewFont "-*-courier-medium-r-normal--*-120-*-*-*-*-*-*"

wm title . "tkview"
wm iconname . "tkview"

frame .view -bd 0 -relief flat
scrollbar .vscroll -command ".text yview" 
text .text -yscroll ".vscroll set" -setgrid 1 -height 20 -width 80 \
     -font $viewFont 

button .quit  -text "Exit" -width 8 -command { exit 0 }

pack .view -expand yes -fill both
pack .text -in .view -side left -expand yes -fill both
pack .vscroll -in .view -side left -fill y
pack .quit -side bottom

set file $argv

set f [open $file r]
set data [read $f]
close $f

.text insert end $data
.text configure -state disabled

focus .vscroll

# eof

And here is a screenshot of the finished application displaying part of my Samba configuration file:

tkview screenshot

Prev - Index