# Request a file name. Assume success. set fileName [tk_getOpenFile] set if [open $fileName r] set data [read $if] close $if # Initialize the variables to empty strings. set lineCount {} set wordCount {} set charCount {} # Create a label to describe the value on this line, # And then a label to hold the value. label .lLine -text "Line Count" label .lLineVal -textvariable lineCount # Add a button to calculate a value to display button .bCountLines -text "Lines" -command {set lineCount [llength [split $data \n]]} grid .lLine -row 1 -column 1 grid .lLineVal -row 1 -column 2 grid .bCountLines -row 1 -column 3 # Repeat this process for counting words - every word is a list element label .lWord -text "Word Count" label .lWordVal -textvariable wordCount button .bCountWords -text "Words" -command {set wordCount [llength $data ]} grid .lWord -row 2 -column 1 grid .lWordVal -row 2 -column 2 grid .bCountWords -row 2 -column 3 # If split is told to split on an empty string ( {} ) it makes a list in # which each character is a separate list element. label .lChar -text "Char Count" label .lCharVal -textvariable charCount button .bCountChars -text "Chars" -command {set charCount [llength [split $data {}]]} grid .lChar -row 3 -column 1 grid .lCharVal -row 3 -column 2 grid .bCountChars -row 3 -column 3