wdb The procedure canvasdump provides an easy way to save and restore contents of a canvas. Simplifies copying of canvas contents.
Example of storing:
% set canvasContents [canvasdump .c] canvas { list\ [$canvas create rectangle {10.0 10.0 100.0 100.0} -fill yellow -width 5.0]\ [$canvas create oval {50.0 50.0 150.0 150.0} -fill red -width 5.0] }
Restoring:
% toplevel .top .top % pack [canvas .top.c] % apply $canvasContents .top.c 1 2 % # .top.c contains drawing of .c; result is list of IDs
Code:
proc canvasdump canvas { set items "" foreach item [$canvas find all] { set cmd create lappend cmd [$canvas type $item] set coords {} foreach x [$canvas coords $item] { lappend coords [expr [format %f $x]] } lappend cmd $coords foreach li [$canvas itemconfigure $item] { lassign $li key - - def val if {$def != $val} then { if {$key eq "-image"} then { set image [$canvas itemcget $item -image] set file [$image cget -file] append cmd " $key " [subst -nocommand {[image create photo -file "$file"]}] } else { append cmd " $key " [list $val] } } } lappend items " \[\$canvas $cmd\]" } set lines [join $items \\\n]\n concat canvas "{\n list\\\n$lines}" }