Flowers

ulis, created 2003-09-17, Flowered checkbuttons.

Updated 2003-09-20.

KPV: Heaven help you if you ever add comments to your code.

ulis done

KPV: I was referring to comments that appear at the end of a line of code. With your coding style, such comments would break your code.

ulis I don't think so and had no problem with that. Coding style is matter of taste and I hate the style you tried to impose.

Jorge Moreno: This is a very interesting piece of code, if we just could have a more descriptive title, like "Using multiple images in a single canvas"...

RS: A canvas is a container for graphic items, of which images are one kind, and of course you can have multiple ones :)

 pack [canvas .c]
 .c create image 50  50 -image $im1
 .c create image 150 50 -image $im2

A more descriptive title would be : "Using pictures as radiobuttons"

JM Sep-25-2005 agree.

As a side note, I am using an application, mostly based in this code, that is basically an electronic checklist, which take advantage of this overlapping of pictures, make them a sequence, to recall the user to include all accesories that are part of a product we are packing. This one practical use of a simple, short, clever piece of code.


3 flowers for ulis after more than 6 years he left us...Thanks.
flowersDOTtcl


The script

  # Please, download the images files in the current directory before executing the script.
  # http://perso.wanadoo.fr/maurice.ulis/tcl/flower-1.gif
  # http://perso.wanadoo.fr/maurice.ulis/tcl/flower-2.gif
  # http://perso.wanadoo.fr/maurice.ulis/tcl/flower-3.gif

  # --------------------------------
  #
  # flowers
  #
  # --------------------------------

  package require Tk
  
  # -------------
  # parameters
  # -------------

  #  name      color       file
  set parms \
  {
    {flower1   DeepPink3   flower-1.gif}
    {flower2   orange      flower-2.gif}
    {flower3   blue        flower-3.gif}
  }

  # -------------
  # nmage/bimage packages
  # -------------

  # create a canvas with n images
  proc nmage {w args} \
  {
    # take size from the first image
    set image [lindex $args 0]
    set ww [image width $image]
    set hh [image height $image]
    # center coordinates
    set x [expr {$ww / 2}]
    set y [expr {$hh / 2}]
    # create canvas
    canvas $w -width $ww -height $hh -highlightt 0
    # create the images
    foreach image $args \
    { $w create image $x $y -anchor center -image $image }
  }
  # create a canvas with a mini and a micro image
  proc bimage {n} \
  {
    # take size from the full image
    set image $::names($n)
    set ww [image width $image]
    set hh [image height $image]
    # create the mini & the micro image from the full image
    foreach {type factor} {mini 4 micro 8} \
    {
      set ww2 [expr {$ww / $factor}]
      set hh2 [expr {$hh / $factor}]
      image create photo ${type}$n -width $ww2 -height $hh2
      ${type}$n copy $image -subsample $factor
    }
    # create a canvas with the mini & micro images
    nmage .$n mini$n micro$n
  }

  # -------------
  # mechanism
  # -------------

  # called on n-th choice (click on canvas)
  proc select {n} \
  {
    # select the little image
    foreach i $::list { choose $i $n }
    # show the n-th image
    foreach i $::list { .0 itemconf $i -state hidden }
    .0 itemconf $n -state normal
    # show its name
    .name config -text $::names($n)
    # set background color
    set color $::colors($n)
    foreach w {. .name .0} { $w config -bg $color }
    foreach i $::list { .$i config -bg $color }
  }
  # choose between the mini & the micro image
  proc choose {i n} \
  {
    set a 1; set b 2
    if {$i == $n} { set a 2; set b 1 }
    .$i itemconf $a -state hidden
    .$i itemconf $b -state normal
  }

  # -------------
  # create images
  # -------------

  set i 0
  foreach item $parms \
  { 
    foreach {name color file} $item break
    lappend list [incr i] 
    set names($i) $name
    set colors($i) $color
    set files($i) $file
  }
  foreach i $list { image create photo $names($i) -file $files($i) }

  # -------------
  # create widgets
  # -------------

  # the canvas with the full images
  foreach i $list { lappend images $names($i) }
  eval [linsert $images 0 nmage .0]
  # the label with the name
  label .name -font {Times -24}
  # the selection canvas
  foreach i $list \
  {
    # canvas with a mini and a micro image
    bimage $i
    # call change on click
    bind .$i <1> [list select $i]
  }

  # -------------
  # place & display widgets
  # -------------

  grid .0    -row 0 -column 1 -columnspan 3
  grid .name -row 1 -column 1 -columnspan 3
  foreach i $list { grid .$i -row 2 -column $i }

  # -------------
  # do it!
  # -------------

  wm title . flowers
  select 1