Version 8 of tiled image

Updated 2003-07-10 17:00:05

[...]

What is a tiled image? A tiled image is the behavior of a widget to display one or more repetitions of an image. The number of repetitions depends on the size of the widget and the size of the image.

When would I use a tiled image? If one wanted a background that had a textured feel to it, a small image , tiled, can provide that sort of optical illusion.

BLT (?) and Xbit support tiled images. With the latter,

    set tiled_image [image create gimg tiledImage -file image_file_name -tile 1]

can be used anywhere that a conventional Tk image is valid (true?).


Saverio Castellano offers,

    proc SetTiledBackground {cv img} {
      set h [image height $img]
      set w [image width $img]
      set width [$cv cget -width]
      set height [$cv cget -height]
      set row [expr $h/2]
      set col [expr $w/2]

      while {$row<$height} {
        set col [expr $w/2]
        while {$col<$width} {
           $cv create image $col $row -image $img
           incr col $w
        }
        incr row $h
      }
    }

Bryan Oakley (09 July 2003) provides a different variation which can be bound to the widget's <Configure> event so it will be retiled as the widget grows

        # usage: tile canvasWidget ?image?
        proc tile {canvas args} {

                set oldImage [$canvas itemcget tile -image]

                switch -exact [llength $args] {
                        0 {set image $oldImage}
                        1 {set image [lindex $args 0]}
                        default {
                                return -code error "usage: tile path ?image?"
                        }
                }

                if {$image != $oldImage 
                    || [string length $image] == 0} {
                        $canvas delete all
                }

                if {[string length $image] == 0} return

                set imageHeight [image height $image]
                set imageWidth [image width $image]

                set width [winfo width $canvas]
                set height [winfo height $canvas]

                set x 0
                while {$x < $width} {
                        set y 0
                        while {$y < $height} {
                                # see if there's already a tile here
                                set tags [$canvas gettags $x/$y]
                                if {$tags == ""} {
                                        $canvas create image $x $y \
                                            -image $image \
                                            -anchor nw \
                                            -tags [list tile $x/$y] \
                                    }
                                incr y $imageHeight
                        }
                        incr x $imageWidth
                }

        }

Here's some code to test it:

        image create photo question -data {
                R0lGODlhFgAWAIUAAPwCBAQCBCQuNBwiJAwiLAwaJAwSHAwSFIy+3ERynCw2
                PCQuPAwmPCxOZCxWdJzG3FSazBwmNAQKDAQGBDRmhBQyTDxujDR2rIy21AwW
                JDyGxCxmjAwmNDRihAQOFDxmhCxunBQWFAwaLCRahDR6rESGvDQ2PCRWdDRu
                nDSGvCRSdAwWHCwuLDSOzHSmxDyKxBxCZBwqNHSu1DyOzAQSHAAAAAAAAAAA
                AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAWABYAAAah
                QIBwCAgIBAPCoGAgOoeBAyKhWCwYDUf0CX1AIhLiJEGpBLiAAaRxdgYsl7Yb
                k8igBZoN5xmAdDxoanp8HyANISF8EBsiXBMjJBolBEQmGHFoRScbKHIKDykq
                K5lFAZRCnyknTaROLA8tq61OChgtKqyzQgEYEJi6UC4vI3LAASkbMBPARAEB
                dszR0sACEaPSMTIQM8W6KzNl3bo0NOJDdEEAIf5oQ3JlYXRlZCBieSBCTVBU
                b0dJRiBQcm8gdmVyc2lvbiAyLjUNCqkgRGV2ZWxDb3IgMTk5NywxOTk4LiBB
                bGwgcmlnaHRzIHJlc2VydmVkLg0KaHR0cDovL3d3dy5kZXZlbGNvci5jb20A
                Ow==
        }
        canvas .c
        tile .c question
        bind .c <Configure> [list tile %W]
        pack .c -side top -fill both -expand y

DKF: I prefer to create a single pre-tiled image of the required size when the canvas isn't too large; the memory consumption is more, but the script is much shorter...

 image create photo foo -data {...}
 $canvas create image 0 0 -anchor nw -image [image create photo foo-tiled]
 foo-tiled copy foo -to 0 0 [winfo width $canvas] [winfo height $canvas]
 # Yes, that's all it takes folks!  photo image copying tiles for you!

(Bryan Oakley sez: very impressive! Nice job, Donal)


Category GUI