Welcome, guest | Sign In | My Account | Store | Cart

A Text File Viewer Program Utilizing a Text Widget With A Vertical Scrollbar. Menu And Button Widgets Are Used In Conjunction With a tk_getOpenFile Dialog Window To Allow The User To Select A Text File For Viewing. A Menu Option And Button Widget Enable A Program Exit Option.

Tcl, 121 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# PROGRAM-NAME: VW.TCL              VERSION NO.: 1.01
# AUTHOR: Tony Dycks                REVISED-BY: Tony Dycks
# DATE-WRITTEN: August 7, 2001      DATE-REVISED: September 10, 2001
# LANGUAGE: Active State Tcl/Tk     VERSION. NO.: 8.3
# PLATFORMS TESTED: MS Windows 98/NT 4.0
#                   Red Hat Linux 7.1 / SuSE Linux 7.0
#
# DESCRIPTION:
#   Tcl/Tk Wish Script For Viewing A Text File Using A TK Window.
#   File Contents Are Displayed On Text Widget Using TK As A GUI.
#   A Scrollbar Widget Is Added To Allow The Viewing Of File Contents.
#   Program Uses A File Menu Options To Select Files For Viewing 
#   And To Exit The Program. Clicking The "Exit Program" Button Closes
#   The TK Window And Ends The Program. Bind To <F3> Function Key
#   To Exit The Program.
#
# USAGE:
#   WISH VW.TCL <Enter>
#     {Where WISH is a shell or bat file invoking the WISH executable}
#
# USAGE EXAMPLE:
#   WISH VW.TCL <Enter>  
#
# REFERENCES: 
#   Personal Derivation Of TCL/TK Code Using The Grid Layout Manager. 
#
# LICENSING: 
#   Released Under The GPL As Open Source. 
#
# +---------------------------------------------------+
# + Set Initial Directory To Current Logged Directory +
# +---------------------------------------------------+
set initialdir [pwd]
global initialdir
global flname
# +------------------------------------------------------+
# + Select Open Input Text File & Populate Entry Widgets +
# +------------------------------------------------------+
proc openfl {} {
  global initialdir
  global flname
  set file_types {
    {"Tcl Files" { .tcl .TCL } }
    {"Text Files" { .txt .TXT } }
    {"All Files"  * }
    }
# +-------------------------------------------------+
# + Cleanup Filename And Text File Contents Widgets +
# +-------------------------------------------------+
  .txtarea delete 1.0 end
  set flname [tk_getOpenFile -initialdir $initialdir     -filetypes $file_types -title "Open Input Text File" -parent .]
  if {$flname != ""} {
    set initialdir [file dirname $flname]
    set retcd [ catch { set infile [open $flname "r"] } ]
# +------------------------------------------------+
# + Display Error Message Box If File Open Failure +
# +------------------------------------------------+
    if {$retcd == 1} {
      wm title . "VW.TCL -- File Open Error Message"
      set result [tk_messageBox -parent .         -title "VW.TCL -- File Open Error Message" -type ok -icon error         -message         "Error Opening File: $flname.\n"]
      }
# +----------------------------------------------+
# + Open File Successful Load Text File Contents +
# + Line By Line Until End Of File               +
# +----------------------------------------------+
    if {$retcd == 0} {
      set inEOF -1
      set txln ""
      .txtarea delete 1.0 end
      while {[gets $infile inln] != $inEOF} {
        set txln "$inln\n"
        .txtarea insert end $txln
        }
      close $infile
      }
    }
  return $flname
  }
# +------------------+
# + Exit The Program +
# +------------------+
proc exitpgm {} {
  exit 0
  }

# +-------------------------------------------------+
# + Initial TK Widget Definitions For Viewer Window +
# +-------------------------------------------------+
wm title . "VW.TCL Version 1.01 -- Text File Viewer Tcl/Tk Progam"
# +--------------+
# + Menu Widgets +
# +--------------+
menubutton .fl -text "File" -menu .fl.menu -anchor nw
menu .fl.menu
.fl.menu add command -label "Open" -command openfl
.fl.menu add separator
.fl.menu add command -label "Exit" -command exitpgm
set font {Verdana 14}
# +------------------------+
# + Filename Label Widgets +
# +------------------------+
label .fllabel -text "Input Filename:" -relief sunken -bg NavajoWhite2   -fg Navy -anchor nw
label .flname -width 80 -relief sunken -bg NavajoWhite2   -fg Navy -textvariable flname -anchor nw
pack .fl .fllabel .flname -side top -padx 1m -pady 1m -anchor nw
# +----------------------------------------+
# + Text File Contents & Scrollbar Widgets +
# +----------------------------------------+
label .fltext -width 80 -relief sunken -bg White -textvariable fltext
text .txtarea -bg LightYellow2 -font FixedSys -bd 2   -yscrollcommand ".vscroller set"
scrollbar .vscroller -command ".txtarea yview"
pack .txtarea .vscroller -side left -fill y
# +-----------------------------------------------------+
# + Command Button Widgets For Open File & Program Exit +
# +-----------------------------------------------------+
button .openfl -text "<< Open File >>" -fg Navy -bg NavajoWhite2   -font bold -command openfl
button .exitpgm -text "< Exit Program >" -fg Navy -bg NavajoWhite2   -font bold -command exitpgm
pack .exitpgm .openfl -side bottom -padx 1m -pady 1m 

bind .txtarea <Key-F3> {exitpgm}
bind .fllabel <Key-F3> {exitpgm}
bind .flname <Key-F3> {exitpgm}

Provides a alternate platform independent utility for viewing text files (I use this utility on a Windows 98 SE, Windows NT 4.0, Red Hat Linux 7.1 and SuSE Linux 7.1 environments).

One benefit using the viewer versus Notepad or an another text edit program is that by opening the file as read-only, there is no risk for inadvertant modification of the file.

Since the file contents are stored in a text widget, copy, cut and paste clipboard functions can be performed under Windows.

The program is also a fair demonstration of the use of TK widgets and makes for a good starting point for an elementary Text Edit program utility written in TCL/TK.

I found "Graphical Applications With Tcl & TK" 2nd Edition by Eric Foster-Johnson ((c)1997 by M & T Books, ISBN #1-55851-569-0) to be a great reference in helping me develop this program utility. The author's website can be referenced at URL, http://www.pconline.com/~erc, and covers topics such as Tcl/TK, Linux, Perl and Motif.

Created by Tony Dycks on Mon, 10 Sep 2001 (MIT)
Tcl recipes (162)
Tony Dycks's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks