| |||||||||||
| |||||||||||
itcl and tk together - full app
this example from a Well House Consultants training course
Source code: distance_graph Module: T050
#!/usr/bin/env tclsh
set decription { Reads in data files from the October 2014 TransWilts Passenger Surveys, and rail station information for OS grid references for all 2537 stations on the national network. Works out the journey distance for each journey reported during the survey from each of the TransWilts Stations (and also for journeys that were through journeys) and allows the user to select via a Tk GUI which stations to display a cumulative display of journey lengths } # ------------------------------------------------------------------------- proc myputs {args} { # cure "-nonewline" funnines in a funny way # debug capability if {1 == 0} { set temp [puts $args] } } package require Tk package require Itcl namespace import itcl::* # Passenger flows class flows { private variable journeys constructor {} { # set journeys [array] } method addJourney {place distance} { lappend journeys($place) $distance } method getLengths {place} { return [lsort -real $journeys($place)] } } # Network Rail Stations class nrstation { public variable f public variable tlc constructor record { set f [split [string trim $record] \t] set tlc [lindex $f 1] } method getOS {} { regsub " " [lindex $f 3] "" rv return $rv } } # Individual journey (survey response) objects class journey { public variable f public variable complete private common stationsloaded {} private common stationtable constructor record { set f [split [string trim $record] \t] } method getEnds {} { set rv [list [lindex $f 5] [lindex $f 13]] set complete 1 foreach end $rv { if {$end == ""} {set complete 0} } return $rv } method getFar {{from {}}} { if {$from != {}} { set farend {} if {[lindex $f 5] == $from} {set farend [lindex $f 13]} if {[lindex $f 13] == $from} {set farend [lindex $f 5]} } else { set farend {} } return $farend } method getDistance {} { if {$stationsloaded == ""} { set fh [open rstats2014.xyz r] set count 0 while {[gets $fh lyne] > 0} { set sinfo [nrstation nr_[incr count] $lyne] set tlc [$sinfo cget -tlc] set stationtable($tlc) $sinfo } set stationsloaded 1 } set ee [$this getEnds] set aok 1 foreach end $ee { if {[info exists stationtable($end)]} { lappend rv [$stationtable($end) getOS] } else { lappend rv "missing" set aok 0 set disty 0 } } if {$aok} { set disty [getkms [lindex $rv 0] [lindex $rv 1]] } myputs "$ee $rv $disty" return $disty } } # -------------------------------------------------------------------------- # Finding distances between places base on OS grid references # Read in the OS big grid table (initialisation) # May have been an idea to have an OS reference object (hindsight is wonderful) set fh [open os_letters.txt r] while {[gets $fh lyne] > 0} { regsub "#.*" $lyne "" result if {[string length $result] < 1} continue lappend squares $result } # Locate the places and get the distance proc getkms {from to} { global squares foreach osref "$from $to" { # Need to trap erroneous formats in the user's grid refs! set letters [string range $osref 0 1] # Scan to sort out issues with leading zeros on easting / northing myputs $osref scan [string range $osref 2 4] %d east scan [string range $osref 5 end] %d north set mega_north 0 foreach band $squares { if {[set mega_east [lsearch $band $letters]] > -1} break incr mega_north } set easting [expr 1000 * $mega_east + $east] set northing [expr 1000 * $mega_north + $north] lappend elist $easting lappend nlist $northing } # And work out the length of each leg for {set to 1} {$to < [llength $elist]} {incr to} { set from [expr $to - 1] set east_west [expr [lindex $elist $from] - [lindex $elist $to]] set north_south [expr [lindex $nlist $from] - [lindex $nlist $to]] set leg_distance [format %.1f [expr 0.1 * hypot($east_west, $north_south)]] myputs $leg_distance } return $leg_distance } # -------------------------------------------------------------------------- # ------- HERE COMES THE MAIN CODE! # ---- read and store all the journeys set fh [open export_0.97 r] set count 0 while {[gets $fh lyne] > 0} { lappend jlist [journey j_[incr count] $lyne] } # ---- Initialise an empty flows object flows transWilts # ---- Add the journeys to the flows foreach j $jlist { myputs [$j getEnds] if {[$j cget -complete] == 0} {myputs "incomplete"} else { set local 0 set kms [$j getDistance] foreach station {SWI CPM MKM TRO WSB} { set away [$j getFar $station] if {$away != ""} { myputs "($station to $away)" set local 1 # puts "from $station by $kms" transWilts addJourney $station $kms } } if {$local == 0} { myputs "through journey" # puts "through journey of $kms" transWilts addJourney thru $kms } myputs "Journey length $kms" } } # --------------------------------------------------------------------------- # ---- Create the look and feel of the GUI # Buttons down the left hand side # SWI CPM MKM TRO WSB and quit # A canvas to the right - 400 wide / 200 tall # Setting the canvas to go no smaller # Application constants! set blist {thru SWI CPM MKM TRO WSB} set showing {0 0 0 0 0 0} set cwidth 450 set cheight 250 # ---- Action when a button is pressed proc inform {place} { global cwidth global cheight global blist global showing set colours {#F88 #8F8 #88F #FF8 #8FF #F8F} # Toggle the appropriate element of the "showing" list set posn [lsearch $blist $place] set showing [lreplace $showing $posn $posn [expr 1 - [lindex $showing $posn]]] # clear the middle of the canvas .right.graph create rect 25 25 [expr $cwidth-25] [expr $cheight-25] \ -outline #000 -fill #345 # draw axes for {set x 80} {$x < [expr $cwidth - 50]} {incr x 80} { .right.graph create line [expr 25 + $x] 25 [expr 25 + $x] [expr $cheight - 25] -fill #123 } for {set y 50} {$y < [expr $cheight - 50]} {incr y 50} { .right.graph create line 25 [expr 25 + $y] [expr $cwidth - 25] [expr 25 + $y] -fill #123 } # Draw the data lines that are required for {set p 0} {$p < [llength $blist]} {incr p} { if {[lindex $showing $p]} { set showplace [lindex $blist $p] set values [transWilts getLengths $showplace] set colour [lindex $colours $p] set oldx 0 set oldy 0 for {set x 0} {$x < [expr $cwidth - 50]} {incr x} { set shorter 0 foreach val $values { if {$val < $x} {incr shorter} } set y [expr ($cheight - 50.0) * $shorter / [llength $values]] .right.graph create line [expr 25+$oldx] [expr 25+$oldy] [expr 25+$x] [expr 25+$y] \ -fill $colour -width 3 set oldx $x set oldy $y } } } } # ------- Define and pack the widgets frame .left frame .right pack .left .right -side left -expand yes -fill both foreach b $blist { pack [button .left.[string tolower $b] -text $b -command "inform $b"] -side top } pack [button .left.quit -text quit -command exit] -side bottom pack [canvas .right.graph -width $cwidth -height $cheight -bg #CCBBAA] \ -side right -expand yes -fill both # Beware - canvas width and height CAN change with the -fill both! # make it display before you ask how big it is! update idletasks set size [wm geometry .] # then set limits based on that size regexp {(\d+)x(\d+)} $size all w h wm aspect . $w $h $w $h wm minsize . $w $h wm maxsize . [expr int($w*1.5)] [expr int($h*1.5)] wm title . "Journey numbers v Journey distances" Learn about this subject
This module and example are covered as required on private courses.
Should you wish to cover this example and associated subjects, and you're attending a public course
to cover other topics with us, please see our extra topic program.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Tcl, Tk and Expect are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "this" training module. You'll find a description of the topic and some
other closely related examples on the "this" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |