diff options
Diffstat (limited to 'tktable/demos')
-rwxr-xr-x | tktable/demos/basic.tcl | 61 | ||||
-rwxr-xr-x | tktable/demos/buttons.tcl | 82 | ||||
-rwxr-xr-x | tktable/demos/command.tcl | 85 | ||||
-rwxr-xr-x | tktable/demos/debug.tcl | 112 | ||||
-rwxr-xr-x | tktable/demos/dynarows.tcl | 87 | ||||
-rwxr-xr-x | tktable/demos/loadtable.tcl | 52 | ||||
-rwxr-xr-x | tktable/demos/maxsize.tcl | 76 | ||||
-rwxr-xr-x | tktable/demos/spreadsheet.tcl | 122 | ||||
-rwxr-xr-x | tktable/demos/tcllogo.gif | bin | 0 -> 2341 bytes | |||
-rw-r--r-- | tktable/demos/tktable.py | 344 | ||||
-rwxr-xr-x | tktable/demos/valid.tcl | 95 |
11 files changed, 1116 insertions, 0 deletions
diff --git a/tktable/demos/basic.tcl b/tktable/demos/basic.tcl new file mode 100755 index 0000000..0d9a0ab --- /dev/null +++ b/tktable/demos/basic.tcl @@ -0,0 +1,61 @@ +#!/bin/sh +# the next line restarts using wish \ +exec wish "$0" ${1+"$@"} + +## basic.tcl +## +## This demo shows the basic use of the table widget +## +## jeff at hobbs org + +source [file join [file dirname [info script]] loadtable.tcl] + +array set table { + rows 8 + cols 8 + table .t + array t +} + +proc fill { array x y } { + upvar $array f + for {set i -$x} {$i<$x} {incr i} { + for {set j -$y} {$j<$y} {incr j} { set f($i,$j) "r$i,c$j" } + } +} + +## Test out the use of a procedure to define tags on rows and columns +proc rowProc row { if {$row>0 && $row%2} { return OddRow } } +proc colProc col { if {$col>0 && $col%2} { return OddCol } } + +label .label -text "TkTable v1 Example" + +fill $table(array) $table(rows) $table(cols) +table $table(table) -rows $table(rows) -cols $table(cols) \ + -variable $table(array) \ + -width 6 -height 6 \ + -titlerows 1 -titlecols 2 \ + -roworigin -1 -colorigin -2 \ + -yscrollcommand {.sy set} -xscrollcommand {.sx set} \ + -rowtagcommand rowProc -coltagcommand colProc \ + -colstretchmode last -rowstretchmode last \ + -selectmode extended -sparsearray 0 + +scrollbar .sy -command [list $table(table) yview] +scrollbar .sx -command [list $table(table) xview] -orient horizontal +button .exit -text "Exit" -command {exit} + +grid .label - -sticky ew +grid $table(table) .sy -sticky news +grid .sx -sticky ew +grid .exit -sticky ew -columnspan 2 +grid columnconfig . 0 -weight 1 +grid rowconfig . 1 -weight 1 + +$table(table) tag config OddRow -bg orange -fg purple +$table(table) tag config OddCol -bg brown -fg pink + +$table(table) width -2 7 -1 7 1 5 2 8 4 14 + +puts [list Table is $table(table) with array [$table(table) cget -var]] + diff --git a/tktable/demos/buttons.tcl b/tktable/demos/buttons.tcl new file mode 100755 index 0000000..29ffddf --- /dev/null +++ b/tktable/demos/buttons.tcl @@ -0,0 +1,82 @@ +#!/bin/sh +# next line is a comment in tcl \ +exec wish "$0" ${1+"$@"} + +## buttons.tcl +## +## demonstrates the simulation of a button array +## +## ellson@lucent.com +## modifications made by jeff at hobbs org + +source [file join [file dirname [info script]] loadtable.tcl] + +array set table { + rows 20 + cols 20 + table .table +} + +# create the table +set t $table(table) +table $t -rows [expr {$table(rows)+1}] -cols [expr {$table(cols)+1}] \ + -titlerows 1 -titlecols 1 \ + -roworigin -1 -colorigin -1 \ + -colwidth 4 \ + -width 8 -height 8 \ + -variable tab \ + -flashmode off \ + -cursor top_left_arrow \ + -borderwidth 2 \ + -state disabled \ + -xscrollcommand ".sx set" -yscrollcommand ".sy set" + +scrollbar .sx -orient h -command "$t xview" +scrollbar .sy -orient v -command "$t yview" + +grid $t .sy -sticky nsew +grid .sx -sticky ew +grid columnconfig . 0 -weight 1 +grid rowconfig . 0 -weight 1 + +# set up tags for the various states of the buttons +$t tag configure OFF -bg red -relief raised +$t tag configure ON -bg green -relief sunken +$t tag configure sel -bg gray75 -relief flat + +# clean up if mouse leaves the widget +bind $t <Leave> { + %W selection clear all +} + +# highlight the cell under the mouse +bind $t <Motion> { + if {[%W selection includes @%x,%y]} break + %W selection clear all + %W selection set @%x,%y + break + ## "break" prevents the call to tkTableCheckBorder +} + +# mousebutton 1 toggles the value of the cell +# use of "selection includes" would work here +bind $t <1> { + set rc [%W cursel] + if {[string match ON $tab($rc)]} { + set tab($rc) OFF + %W tag celltag OFF $rc + } { + set tab($rc) ON + %W tag celltag ON $rc + } +} + +# inititialize the array, titles, and celltags +for {set i 0} {$i < $table(rows)} {incr i} { + set tab($i,-1) $i + for {set j 0} {$j < $table(cols)} {incr j} { + if {! $i} {set tab(-1,$j) $j} + set tab($i,$j) "OFF" + $t tag celltag OFF $i,$j + } +} diff --git a/tktable/demos/command.tcl b/tktable/demos/command.tcl new file mode 100755 index 0000000..e0da582 --- /dev/null +++ b/tktable/demos/command.tcl @@ -0,0 +1,85 @@ +#!/bin/sh +# the next line restarts using wish \ + exec wish "$0" ${1+"$@"} + +## command.tcl +## +## This demo shows the use of the table widget's -command options +## +## jeff at hobbs org + +source [file join [file dirname [info script]] loadtable.tcl] + +array set table { + rows 10 + cols 10 + table .table + array DATA +} + +proc fill { array x y } { + upvar $array f + for {set i -$x} {$i<$x} {incr i} { + for {set j -$y} {$j<$y} {incr j} { set f($i,$j) "$i x $j" } + } +} + +## Test out the use of a procedure to define tags on rows and columns +proc rowProc row { if {$row>0 && $row%2} { return OddRow } } +proc colProc col { if {$col>0 && $col%2} { return OddCol } } + +proc tblCmd { arrayName set cell val } { + upvar \#0 $arrayName data + + if {$set} { + #echo set $cell $val + set data($cell) $val + } else { + #echo get $cell + if {[info exists data($cell)]} { + return $data($cell) + } else { + return + } + } +} + +label .label -text "TkTable -command Example" +label .current -textvar CURRENT -width 5 +entry .active -textvar ACTIVE + +bind .active <Return> "$table(table) curvalue \[%W get\]" + +fill $table(array) $table(rows) $table(cols) +set t $table(table) +table $table(table) -rows $table(rows) -cols $table(cols) \ + -command [list tblCmd $table(array) %i %C %s] -cache 1 \ + -width 6 -height 6 \ + -titlerows 1 -titlecols 1 \ + -yscrollcommand {.sy set} -xscrollcommand {.sx set} \ + -roworigin -1 -colorigin -1 \ + -rowtagcommand rowProc -coltagcommand colProc \ + -selectmode extended \ + -rowstretch unset -colstretch unset \ + -flashmode on -browsecommand { + set CURRENT %S + set ACTIVE [%W get %S] +} -validate 1 -validatecommand { + set ACTIVE %S + return 1 +} + +scrollbar .sy -command [list $table(table) yview] -orient v +scrollbar .sx -command [list $table(table) xview] -orient h +grid .label - - -sticky ew +grid .current .active - -sticky ew +grid $table(table) - .sy -sticky nsew +grid .sx - -sticky ew +grid columnconfig . 1 -weight 1 +grid rowconfig . 2 -weight 1 + +$table(table) tag config OddRow -bg orange -fg purple +$table(table) tag config OddCol -bg brown -fg pink + +puts [list Table is $table(table)] + diff --git a/tktable/demos/debug.tcl b/tktable/demos/debug.tcl new file mode 100755 index 0000000..c384920 --- /dev/null +++ b/tktable/demos/debug.tcl @@ -0,0 +1,112 @@ +#!/bin/sh +# the next line restarts using wish \ +exec wish "$0" ${1+"$@"} + +## version2.tcl +## +## This demo uses most features of the table widget +## +## jeff at hobbs org + +source [file join [file dirname [info script]] loadtable.tcl] + +array set table { + rows 25 + cols 20 + table .t + array t +} + +proc fill { array x y } { + upvar $array f + for {set i -$x} {$i<$x} {incr i} { + for {set j -$y} {$j<$y} {incr j} { set f($i,$j) "r$i,c$j" } + } +} + +## Test out the use of a procedure to define tags on rows and columns +proc colProc col { if {$col > 0 && $col % 2} { return OddCol } } + +label .label -text "TkTable v2 Example" + +fill $table(array) $table(rows) $table(cols) +table $table(table) \ + -rows $table(rows) -cols $table(cols) \ + -variable $table(array) \ + -width 6 -height 8 \ + -titlerows 1 -titlecols 2 \ + -roworigin -5 -colorigin -2 \ + -yscrollcommand {.sy set} \ + -xscrollcommand {.sx set} \ + -coltagcommand colProc \ + -selectmode extended \ + -rowstretch unset \ + -colstretch unset \ + -selecttitles 0 \ + -drawmode single + +scrollbar .sy -command [list $table(table) yview] +scrollbar .sx -command [list $table(table) xview] -orient horizontal +button .exit -text "Exit" -command {exit} +grid .label - -sticky ew +grid $table(table) .sy -sticky news +grid .sx -sticky ew +grid .exit -sticky ew -columnspan 2 +grid columnconfig . 0 -weight 1 +grid rowconfig . 1 -weight 1 + +$table(table) tag config OddCol -bg brown -fg pink +$table(table) tag config title -bg red -fg green -relief sunken +$table(table) tag config dis -state disabled + +set i -1 +set first [$table(table) cget -colorigin] +foreach anchor {n s e w nw ne sw se c} { + $table(table) tag config $anchor -anchor $anchor + $table(table) tag row $anchor [incr i] + $table(table) set $i,$first $anchor +} +font create courier -family Courier -size 10 +$table(table) tag config s -font courier -justify center + +image create photo logo \ + -file [file join [file dirname [info script]] tcllogo.gif] +$table(table) tag config logo -image logo -showtext 1 +$table(table) tag cell logo 1,2 2,3 4,1 +$table(table) tag cell dis 2,1 1,-1 3,0 +$table(table) width -2 8 -1 9 0 12 4 14 + +$table(table) set \ + 1,1 "multi-line\ntext\nmight be\ninteresting" \ + 3,2 "more\nmulti-line\nplaying\n" \ + 2,2 "null\0byte" + +set i -1 + +# This is in the row span +set l [label $table(table).s -text "Window s" -bg yellow] +$table(table) window config 6,0 -sticky s -window $l + +# This is in the row titles +set l [label $table(table).ne -text "Window ne" -bg yellow] +$table(table) window config 4,-1 -sticky ne -window $l + +# This will get swallowed by a span +set l [label $table(table).ew -text "Window ew" -bg yellow] +$table(table) window config 5,3 -sticky ew -window $l + +# This is in the col titles +set l [label $table(table).news -text "Window news" -bg yellow] +$table(table) window config -5,1 -sticky news -window $l + +set l [label [winfo parent $table(table)].l -text "Sibling l" -bg orange] +$table(table) window config 5,1 -sticky news -window $l + +if {![catch {$table(table) span}]} { + $table(table) span -1,-2 0,3 1,2 0,5 3,2 2,2 6,0 4,0 +} + +puts [list Table is $table(table) with array [$table(table) cget -var]] + +#$table(table) postscript -file out.ps -first origin -last 2,2 +#if {[string match {} [info commands tkcon]]} exit diff --git a/tktable/demos/dynarows.tcl b/tktable/demos/dynarows.tcl new file mode 100755 index 0000000..9006953 --- /dev/null +++ b/tktable/demos/dynarows.tcl @@ -0,0 +1,87 @@ +#!/bin/sh +# the next line restarts using wish \ +exec wish "$0" ${1+"$@"} + +## dynarows.tcl +## +## This demos shows the use of the validation mechanism of the table +## and uses the table's cache (no -command or -variable) with a cute +## dynamic row routine. +## +## jeff at hobbs org + +source [file join [file dirname [info script]] loadtable.tcl] + +proc table_validate {w idx} { + if {[scan $idx %d,%d row col] != 2} return + set val [$w get $idx] + + ## Entries in the last row are allowed to be empty + set nrows [$w cget -rows] + if {$row == ${nrows}-1 && [string match {} $val]} { return } + + if {[catch {clock scan $val} time]} { + bell + $w activate $idx + $w selection clear all + $w selection set active + $w see active + } else { + set date {} + foreach item [clock format $time -format "%m %d %Y"] { + lappend date [string trimleft $item "0"] + } + $w set $idx [join $date "/"] + if {$row == ${nrows}-1} { + ## if this is the last row and both cols 1 && 2 are not empty + ## then add a row and redo configs + if {[string comp [$w get $row,1] {}] && \ + [string comp [$w get $row,2] {}]} { + $w tag row {} $row + $w set $row,0 $row + $w configure -rows [incr nrows] + $w tag row unset [incr row] + $w set $row,0 "*" + $w see $row,1 + $w activate $row,1 + } + } + } +} + +label .example -text "Dynamic Date Validated Rows" + +set t .table +table $t -rows 2 -cols 3 -cache 1 -selecttype row \ + -titlerows 1 -titlecols 1 \ + -yscrollcommand { .sy set } \ + -xscrollcommand { .sx set } \ + -height 5 -colstretch unset -rowstretch unset \ + -autoclear 1 -browsecommand {table_validate %W %s} + +$t set 0,1 "Begin" 0,2 "End" 1,0 "*" +$t tag config unset -fg \#008811 +$t tag config title -fg red +$t tag row unset 1 +$t width 0 3 + +scrollbar .sy -command [list $t yview] +scrollbar .sx -command [list $t xview] -orient horizontal +grid .example - -sticky ew +grid $t .sy -sticky news +grid .sx -sticky ew +grid columnconfig . 0 -weight 1 +grid rowconfig . 1 -weight 1 + +bind $t <Return> { + set r [%W index active row] + set c [%W index active col] + if {$c == 2} { + %W activate [incr r],1 + } else { + %W activate $r,[incr c] + } + %W see active + break +} +bind $t <KP_Enter> [bind $t <Return>] diff --git a/tktable/demos/loadtable.tcl b/tktable/demos/loadtable.tcl new file mode 100755 index 0000000..c691ac7 --- /dev/null +++ b/tktable/demos/loadtable.tcl @@ -0,0 +1,52 @@ +# loadtable.tcl +# +# Ensures that the table library extension is loaded + +if {[string equal "Windows CE" $::tcl_platform(os)]} { + if {[info proc puts] != "puts" || ![llength [info command ::tcl::puts]]} { + # Rename puts to something innocuous on Windows CE, + # but only if it wasn't already renamed (thus it's a proc) + rename puts ::tcl::puts + proc puts args { + set la [llength $args] + if {$la<1 || $la>3} { + error "usage: puts ?-nonewline? ?channel? string" + } + set nl \n + if {[lindex $args 0]=="-nonewline"} { + set nl "" + set args [lrange $args 1 end] + } + if {[llength $args]==1} { + set args [list stdout [join $args]] ;# (2) + } + foreach {channel s} $args break + if {$channel=="stdout" || $channel=="stderr"} { + #$::putsw insert end $s$nl + } else { + set cmd ::tcl::puts + if {$nl==""} {lappend cmd -nonewline} + lappend cmd $channel $s + uplevel 1 $cmd + } + } + } +} + +set ::VERSION 2.10 +if {[string compare unix $tcl_platform(platform)]} { + set table(library) Tktable$::VERSION[info sharedlibextension] +} else { + set table(library) libTktable$::VERSION[info sharedlibextension] +} +if { + [string match {} [info commands table]] + && [catch {package require Tktable $::VERSION} err] + && [catch {load [file join [pwd] $table(library)]} err] + && [catch {load [file join [pwd] .. unix $table(library)]} err] + && [catch {load [file join [pwd] .. win $table(library)]} err] +} { + error $err +} else { + puts "Tktable v[package provide Tktable] loaded" +} diff --git a/tktable/demos/maxsize.tcl b/tktable/demos/maxsize.tcl new file mode 100755 index 0000000..198eefb --- /dev/null +++ b/tktable/demos/maxsize.tcl @@ -0,0 +1,76 @@ +#!/bin/sh +# the next line restarts using wish \ +exec wish "$0" ${1+"$@"} + +## maxsize.tcl +## +## This demo uses a really big table. The big startup time is in +## filling the table's Tcl array var. +## +## jeff at hobbs org + +source [file join [file dirname [info script]] loadtable.tcl] + +array set table { + rows 40000 + cols 10 + table .t + array t +} + +proc fill { array x y } { + upvar $array f + for {set row 0} {$row<$x} {incr row} { + for {set col 0} {$col<$y} {incr col} { + set f($row,$col) "$row,$col" + } + } +} + +## Test out the use of a procedure to define tags on rows and columns +proc colProc col { if {$col > 0 && $col % 2} { return OddCol } } + +label .label -text "TkTable v2 Example" + +fill $table(array) $table(rows) $table(cols) +table $table(table) \ + -rows $table(rows) -cols $table(cols) \ + -variable $table(array) \ + -width 6 -height 8 \ + -titlerows 1 -titlecols 1 \ + -yscrollcommand {.sy set} \ + -xscrollcommand {.sx set} \ + -coltagcommand colProc \ + -selectmode extended \ + -rowstretch unset \ + -colstretch unset \ + -selecttitles 0 \ + -drawmode slow + +scrollbar .sy -command [list $table(table) yview] +scrollbar .sx -command [list $table(table) xview] -orient horizontal +button .exit -text "Exit" -command {exit} +grid .label - -sticky ew +grid $table(table) .sy -sticky news +grid .sx -sticky ew +grid .exit -sticky ew -columnspan 2 +grid columnconfig . 0 -weight 1 +grid rowconfig . 1 -weight 1 + +$table(table) tag config OddCol -bg brown -fg pink +$table(table) tag config title -bg red -fg blue -relief sunken +$table(table) tag config dis -state disabled + +set i -1 +set first [$table(table) cget -colorigin] +foreach anchor {n s e w nw ne sw se c} { + $table(table) tag config $anchor -anchor $anchor + $table(table) tag row $anchor [incr i] + $table(table) set $i,$first $anchor +} +font create courier -family Courier -size 10 +$table(table) tag config s -font courier -justify center + +$table(table) width -2 8 -1 9 0 12 4 14 + +puts [list Table is $table(table) with array [$table(table) cget -var]] diff --git a/tktable/demos/spreadsheet.tcl b/tktable/demos/spreadsheet.tcl new file mode 100755 index 0000000..9a36c7e --- /dev/null +++ b/tktable/demos/spreadsheet.tcl @@ -0,0 +1,122 @@ +#!/bin/sh +# the next line restarts using wish \ + exec wish "$0" ${1+"$@"} + +## spreadsheet.tcl +## +## This demos shows how you can simulate a 3D table +## and has other basic features to begin a basic spreadsheet +## +## jeff at hobbs org + +source [file join [file dirname [info script]] loadtable.tcl] + +array set table { + rows 10 + cols 10 + page AA + table .table + default pink + AA orange + BB blue + CC green +} + +proc colorize num { if {$num>0 && $num%2} { return colored } } + +proc fill {array {r 10} {c 10}} { + upvar \#0 $array ary + for {set i 0} {$i < $r} {incr i} { + for {set j 0} {$j < $c} {incr j} { + if {$j && $i} { + set ary($i,$j) "$array $i,$j" + } elseif {$i} { + set ary($i,$j) "$i" + } elseif {$j} { + set ary($i,$j) [format %c [expr 64+$j]] + } + } + } +} + +proc changepage {w e name el op} { + global $name table + if {[string comp {} $el]} { set name [list $name\($el\)] } + set i [set $name] + if {[string comp $i [$w cget -var]]} { + $w sel clear all + $w config -variable $i + $e config -textvar ${i}(active) + $w activate origin + if {[info exists table($i)]} { + $w tag config colored -bg $table($i) + } else { + $w tag config colored -bg $table(default) + } + $w see active + } +} + +label .example -text "TkTable v1 Spreadsheet Example" + +label .current -textvar table(current) -width 5 +entry .active -textvar $table(page)(active) +label .lpage -text "PAGE:" -width 6 -anchor e +tk_optionMenu .page table(page) AA BB CC DD + +fill $table(page) +fill BB [expr {$table(rows)/2}] [expr {$table(cols)/2}] + +trace var table(page) w [list changepage $table(table) .active] + +set t $table(table) +table $t \ + -rows $table(rows) \ + -cols $table(cols) \ + -variable $table(page) \ + -titlerows 1 \ + -titlecols 1 \ + -yscrollcommand { .sy set } \ + -xscrollcommand { .sx set } \ + -coltagcommand colorize \ + -flashmode on \ + -selectmode extended \ + -colstretch unset \ + -rowstretch unset \ + -width 5 -height 5 \ + -browsecommand {set table(current) %S} + +$t tag config colored -bg $table($table(page)) +$t tag config title -fg red -relief groove +$t tag config blue -bg blue +$t tag config green -bg green +$t tag cell green 6,3 5,7 4,9 +$t tag cell blue 8,8 +$t tag row blue 7 +$t tag col blue 6 8 +$t width 0 3 2 7 + +scrollbar .sy -command [list $t yview] +scrollbar .sx -command [list $t xview] -orient horizontal +button .exit -text "Exit" -command exit + +grid .example - - - - -sticky ew +grid .current .active .lpage .page - -sticky ew +grid $t - - - .sy -sticky ns +grid .sx - - - -sticky ew +grid .exit - - - - -sticky ew +grid columnconfig . 1 -weight 1 +grid rowconfig . 2 -weight 1 +grid config $t -sticky news + +bind .active <Return> [list tkTableMoveCell $t 1 0] + +menu .menu +menu .menu.file +. config -menu .menu +.menu add cascade -label "File" -underline 0 -menu .menu.file +.menu.file add command -label "Fill Array" -command { fill $table(page) } +.menu.file add command -label "Quit" -command exit + +puts [list Table is $table(table) with array [$table(table) cget -var]] + diff --git a/tktable/demos/tcllogo.gif b/tktable/demos/tcllogo.gif Binary files differnew file mode 100755 index 0000000..4603d4f --- /dev/null +++ b/tktable/demos/tcllogo.gif diff --git a/tktable/demos/tktable.py b/tktable/demos/tktable.py new file mode 100644 index 0000000..51a2f9c --- /dev/null +++ b/tktable/demos/tktable.py @@ -0,0 +1,344 @@ +# +# #### OUTDATE MODULE #### +# This has been superceded by the tktable.py that ships in the lib area. +# This is kept for compatibility as the newer wrapper is not 100% compatible. +# #### OUTDATE MODULE #### +# +# This file is taken from the usenet: +# http://groups.google.com/groups?selm=351A52BC.27EA0BE2%40desys.de +# From: Klaus Roethemeyer <klaus.roethemeyer at desys.de> +# +# It is provided here as an example of using Tktable with Python/Tkinter. + +#============================================================================ +# +# MODULE: This module contains the wrapper class for the tktable widget +# +# CREATED: Roethemeyer, 20.01.98 +# +# VERSION: $Id: tktable.py,v 1.1.1.1 2011/03/01 20:00:38 joye Exp $ +# +#============================================================================ + +#============================================================================ +# import modules +#---------------------------------------------------------------------------- +import string, types, Tkinter +#---------------------------------------------------------------------------- + +#============================================================================ +# ArrayVar +#---------------------------------------------------------------------------- +class ArrayVar(Tkinter.Variable): + _default = '' + + def __init__(self, master = None): + Tkinter.Variable.__init__(self, master) + + def get(self, index = None): + if not index: + res = {} + for i in self.names(): + res[i] = self._tk.globalgetvar(self._name, i) + try: del res['None'] + except KeyError: pass + return res + else: + return self._tk.globalgetvar(self._name, index) + + def names(self): + return string.split(self._tk.call('array', 'names', self._name)) + + def set(self, index, value = ''): + if value == None: + value = '' + return self._tk.globalsetvar(self._name, index, value) +#---------------------------------------------------------------------------- + + +#============================================================================ +# Table +#---------------------------------------------------------------------------- +class Table(Tkinter.Widget): + + _switches1 = ('cols', 'holddimensions', 'holdtags', 'keeptitles', 'rows', '-') + _tabsubst_format = ('%c', '%C', '%i', '%r', '%s', '%S', '%W') + _tabsubst_commands = ('browsecommand', 'browsecmd', 'command', + 'selectioncommand', 'selcmd', + 'validatecommand', 'valcmd') + + def __init__(self, master, cnf={}, **kw): + try: + master.tk.call('package', 'require', 'Tktable') + except Tkinter.TclError: + master.tk.call('load', '', 'Tktable') + Tkinter.Widget.__init__(self, master, 'table', cnf, kw) + + def _options(self, cnf, kw = None): + if kw: + cnf = Tkinter._cnfmerge((cnf, kw)) + else: + cnf = Tkinter._cnfmerge(cnf) + res = () + for k, v in cnf.items(): + if v is not None: + if k[-1] == '_': k = k[:-1] + if callable(v): + if k in self._tabsubst_commands: + v = "%s %s" % (self._register(v, self._tabsubst), + string.join(self._tabsubst_format)) + else: + v = self._register(v) + res = res + ('-'+k, v) + return res + + def _tabsubst(self, *args): + tk = self.tk + if len(args) != len(self._tabsubst_format): return args + c, C, i, r, s, S, W = args + e = Tkinter.Event() + e.widget = self + e.c = tk.getint(c) + e.i = tk.getint(i) + e.r = tk.getint(r) + e.C = (e.r, e.c) + try: e.s = tk.getint(s) + except Tkinter.TclError: e.s = s + try: e.S = tk.getint(S) + except Tkinter.TclError: e.S = S + e.W = W + return (e,) + + + def _getCells(self, cellString): + res = [] + for i in string.split(cellString): + res.append(tuple(map(int, string.split(i, ',')))) + return res + + def _getLines(self, lineString): + return map(int, string.split(lineString)) + + def _prepareArgs1(self, args): + args = list(args) + + for i in xrange(len(args)): + if args[i] in self._switches1: + args[i] = "-" + args[i] + + return tuple(args) + + + def activate(self, index): + self.tk.call(self._w, 'activate', index) + + def bbox(self, first, last=None): + return self._getints(self.tk.call(self._w, 'bbox', first, last)) or None + + def border_mark(self, x, y, row=None, col=None): + self.tk.call(self._w, 'border', 'mark', x, y, row, col) + + def border_dragto(self, x, y): + self.tk.call(self._w, 'border', 'dragto', x, y) + + def curselection(self, setValue = None): + if setValue != None: + self.tk.call(self._w, 'curselection', 'set', setValue) + + else: + return self._getCells(self.tk.call(self._w, 'curselection')) + + def delete_active(self, index, more = None): + self.tk.call(self._w, 'delete', 'active', index, more) + + def delete_cols(self, *args): + apply(self.tk.call, (self._w, 'delete', 'cols') + self._prepareArgs1(args)) + + def delete_rows(self, *args): + apply(self.tk.call, (self._w, 'delete', 'rows') + self._prepareArgs1(args)) + + def flush(self, first=None, last=None): + self.tk.call(self._w, 'flush', first, last) + + def get(self, first, last=None): + return self.tk.call(self._w, 'get', first, last) + + def height(self, *args): + apply(self.tk.call, (self._w, 'height') + args) + + def icursor(self, arg): + self.tk.call(self._w, 'icursor', arg) + + def index(self, index, rc = None): + if rc == None: + return self._getCells(self.tk.call(self._w, 'index', index, rc))[0] + else: + return self._getCells(self.tk.call(self._w, 'index', index, rc))[0][0] + + def insert_active(self, index, value): + self.tk.call(self._w, 'insert', 'active', index, value) + + def insert_cols(self, *args): + apply(self.tk.call, (self._w, 'insert', 'cols') + self._prepareArgs1(args)) + + def insert_rows(self, *args): + apply(self.tk.call, (self._w, 'insert', 'rows') + self._prepareArgs1(args)) + + def reread(self): + self.tk.call(self._w, 'reread') + + def scan_mark(self, x, y): + self.tk.call(self._w, 'scan', 'mark', x, y) + + def scan_dragto(self, x, y): + self.tk.call(self._w, 'scan', 'dragto', x, y) + + def see(self, index): + self.tk.call(self._w, 'see', index) + + def selection_anchor(self, index): + self.tk.call(self._w, 'selection', 'anchor', index) + + def selection_clear(self, first, last=None): + self.tk.call(self._w, 'selection', 'clear', first, last) + + def selection_includes(self, index): + return int(self.tk.call(self._w, 'selection', 'includes', index)) + + def selection_set(self, first, last=None): + self.tk.call(self._w, 'selection', 'set', first, last) + + def set(self, *args): + apply(self.tk.call, (self._w, 'set') + args) + + def tag_cell(self, tagName, *args): + result = apply(self.tk.call, (self._w, 'tag', 'cell', tagName) + args) + if not args: return self._getCells(result) + + def tag_cget(self, tagName, option): + return self.tk.call(self._w, 'tag', 'cget', tagName, '-' + option) + + def tag_col(self, tagName, *args): + result = apply(self.tk.call, (self._w, 'tag', 'col', tagName) + args) + if not args: return self._getLines(result) + + def tag_configure(self, tagName, cnf={}, **kw): + if not cnf and not kw: + return self.tk.call(self._w, 'tag', 'configure', tagName) + if type(cnf) == types.StringType and not kw: + return self.tk.call(self._w, 'tag', 'configure', tagName, '-' + cnf) + if type(cnf) == types.DictType: + apply(self.tk.call, + (self._w, 'tag', 'configure', tagName) + + self._options(cnf, kw)) + else: + raise TypeError, "usage: <instance>.tag_configure tagName [option] | [option=value]+" + + def tag_delete(self, tagName): + self.tk.call(self._w, 'tag', 'delete', tagName) + + def tag_exists(self, tagName): + return self.getboolean(self.tk.call(self._w, 'tag', 'exists', tagName)) + + def tag_includes(self, tagName, index): + return self.getboolean(self.tk.call(self._w, 'tag', 'includes', tagName, index)) + + def tag_names(self, pattern=None): + return self.tk.call(self._w, 'tag', 'names', pattern) + + def tag_row(self, tagName, *args): + result = apply(self.tk.call, (self._w, 'tag', 'row', tagName) + args) + if not args: return self._getLines(result) + + def validate(self, index): + self.tk.call(self._w, 'validate', index) + + def width(self, *args): + result = apply(self.tk.call, (self._w, 'width') + args) + if not args: + str = string.replace(result, '{', '') + str = string.replace(str, '}', '') + lst = string.split(str) + x = len(lst) + x2 = x / 2 + return tuple(map(lambda i, j, l=lst: (int(l[i]), int(l[j])), + xrange(x2), xrange(x2, x))) + elif len(args) == 1: + return int(result) + else: + return result + + def xview(self, *args): + if not args: + return self._getdoubles(self.tk.call(self._w, 'xview')) + apply(self.tk.call, (self._w, 'xview') + args) + + def yview(self, *args): + if not args: + return self._getdoubles(self.tk.call(self._w, 'yview')) + apply(self.tk.call, (self._w, 'yview') + args) +#---------------------------------------------------------------------------- + + +#============================================================================ +# Test-Function +#---------------------------------------------------------------------------- +if __name__ == '__main__': + from Tkinter import Tk, Label, Button + import pprint + + prn = pprint.PrettyPrinter(indent = 6).pprint + + def test_cmd(event=None): + if event.i == 0: + return '%i, %i' % (event.r, event.c) + else: + return 'set' + + + def browsecmd(event): + print "event:", event.__dict__ + print "curselection:", test.curselection() + print "active:", test.index('active', 'row') + print "anchor:", test.index('anchor', 'row') + + root = Tk() + #root.tk.call('load', '', 'Tktable') + + var = ArrayVar(root) + for y in range(-1, 4): + for x in range(-1, 5): + index = "%i,%i" % (y, x) + var.set(index, index) + + label = Label(root, text="Proof-of-existence test for Tktable") + label.pack(side = 'top', fill = 'x') + + quit = Button(root, text="QUIT", command=root.destroy) + quit.pack(side = 'bottom', fill = 'x') + + test = Table(root, + rows=10, + cols=5, + state='disabled', + width=6, + height=6, + titlerows=1, + titlecols=1, + roworigin=-1, + colorigin=-1, + selectmode='browse', + selecttype='row', + rowstretch='unset', + colstretch='last', + browsecmd=browsecmd, + flashmode='on', + variable=var, + usecommand=0, + command=test_cmd) + test.pack(expand=1, fill='both') + test.tag_configure('sel', background = 'yellow') + test.tag_configure('active', background = 'blue') + test.tag_configure('title', anchor='w', bg='red', relief='sunken') + root.mainloop() +#---------------------------------------------------------------------------- diff --git a/tktable/demos/valid.tcl b/tktable/demos/valid.tcl new file mode 100755 index 0000000..8a16a11 --- /dev/null +++ b/tktable/demos/valid.tcl @@ -0,0 +1,95 @@ +#!/bin/sh +# the next line restarts using wish \ +exec wish "$0" ${1+"$@"} + +## valid.tcl +## +## This demos shows the use of the validation mechanism of the table +## and uses the table's cache (no -command or -variable) +## +## jeff at hobbs org + +source [file join [file dirname [info script]] loadtable.tcl] + +array set table { + rows 10 + cols 10 + table .table +} + +proc colorize num { + if {$num>0 && $num%2} { return colored } +} + +proc fill_headers {w {r 10} {c 10}} { + for {set i 1} {$i < $r} {incr i} { + $w set $i,0 "$i" + } + for {set j 1} {$j < $c} {incr j} { + if {$j%3==1} { + $w set 0,$j AlphaNum + } elseif {$j%2==1} { + $w set 0,$j Alpha + } elseif {$j} { + $w set 0,$j Real + } + } +} + +proc validate {c val} { + if {$c%3==1} { + ## Alphanum + set expr {^[A-Za-z0-9 ]*$} + } elseif {$c%2==1} { + ## Alpha + set expr {^[A-Za-z ]*$} + } elseif {$c} { + ## Real + set expr {^[-+]?[0-9]*\.?[0-9]*([0-9]\.?e[-+]?[0-9]*)?$} + } + if {[regexp $expr $val]} { + return 1 + } else { + bell + return 0 + } +} + +label .example -text "TkTable v1 Validated Table Example" + +set t $table(table) +table $t \ + -rows $table(rows) \ + -cols $table(cols) \ + -cache 1 \ + -titlerows 1 \ + -titlecols 1 \ + -yscrollcommand { .tsy set } \ + -xscrollcommand { .tsx set } \ + -width 5 -height 5 \ + -coltagcommand colorize \ + -flashmode on \ + -selectmode extended \ + -colstretch unset \ + -rowstretch unset \ + -validate yes \ + -vcmd {if {![%W tag includes title %C]} { validate %c %S } } + +fill_headers $t +$t tag config colored -bg lightblue +$t tag config title -fg red +$t width 0 3 + +scrollbar .tsy -command [list $t yview] +scrollbar .tsx -command [list $t xview] -orient horizontal +button .exit -text "Exit" -command {exit} + +grid .example - -sticky ew +grid $t .tsy -sticky news +grid .tsx -sticky ew +grid .exit - -sticky ew +grid columnconfig . 0 -weight 1 +grid rowconfig . 1 -weight 1 + +puts [list Table is $table(table)] + |