From: jmc on
On 27 juil, 05:23, "Gerald W. Lester" <Gerald.Les...(a)KnG-
Consulting.net> wrote:
> jmc wrote:
> > On 27 juil, 01:24, S...(a)ControlQ.com wrote:
> >> On Mon, 26 Jul 2010, jmc wrote:
> >>> Hello,
> >>> On the console, I create an image, and after, try to verify this image
> >>> is actually created :
> >>> Here is a "copy and paste" for what I did :
> >>> (Images) 32 % image create photo testRawImage -data $rawData
> >>> testRawImage
> >>> (Images) 33 % info exists testRawImage
> >>> 0
> >>> I believe "info exists" (Tcl) is not the right tool to make a check
> >>> for the result of a Tk command, but looking in the doc either for
> >>> "winfo" or "info" command, I can't find anything which suit my need.
> >>> Thanks in advance.
> >>> Jean-Marie
> >> Jean-Marie,
>
> >> I took the slightly different approach of creating the image when
> >> needed, and then caching it, so that you use a simple interface to
> >> reference ALL images ...
>
> >> namespace eval img {
> >>    array set pic {}
>
> >>    proc getImage { clr btn } {
> >>      set idx [format "%s_%s" $clr $btn]
> >>      set ix [lsearch [array names img::pic] $idx]
> >>      if { $ix < 0 } {
> >>        return [img::newImage $clr $btn]
> >>      }
> >>      return $img::pic($idx)
> >>    }
>
> >>    proc newImage { clr btn } {
> >>      set idx [format "%s_%s" $clr $btn]
> >>      set img::pic($idx) {}
> >>      if [file exists images/$clr/$btn.gif ] {
> >>        set img::pic($idx) [image create photo img_$idx]
> >>        $img::pic($idx) read images/$clr/$btn.gif
> >>      }
> >>      return $img::pic($idx)
> >>    }
>
> >> }
>
> >>    grid configure [ttk::button $btn -image [img::getImage $clr $b] -command doSomething ]
> >>         -row $r -column $c
>
> >> This uses namespaces, and creates an array indexed by the colour and name
> >> of the image file ... if the image exists, the pre-created reference is
> >> returned by getImage, and if not, getImage references newImage to create it,
> >> and saves the reference ... In my case it was convenient to index by colour
> >> and name, but this is entirely dependant upon your problem domain ... it is
> >> particularly useful for icons and button labels ...
>
> >> HTH, Cheers,
> >> Rob Sciuk.
>
> > Thanks Rob. Very interesting. I didn't realize it was possible to
> > store a command in an array. Cool !
>
> You are not storing a command in the array -- you are storing the name (i..e.
> string) of a command in the array.
>
> You need to stop thinking in terms of commands vs data.  For example,
> consider to you understand how and why the following works:
>
> set c1 s
> set c2 e
> set c3 t
> set to X
> set value 45
> $c1$c2$c3 $to $value
> puts "X = {$X}"
>
> --
> +------------------------------------------------------------------------+
> | Gerald W. Lester, President, KNG Consulting LLC                        |
> | Email: Gerald.Les...(a)kng-consulting.net                                |
> +------------------------------------------------------------------------+

Ok Gerald. I understand your script like this :

1) When the interpreter reads the 5th line :
a) makes substitution of variables if it can
-> the line become : set X 45
b) re-read the resulting line (second round)
first word is recognised as a command -> execute it
-> value 45 is stored in variable X

2) Read next line
a) no substion can occur (braces)
-> the line remains inchanged
b) re-read the line
first word is recognised as a command -> execute it
-> print on screen the string "X = X"

So, in regard with your comment about commands are not stored in an
array only their names : Ok this is subtily different.
But the result (via substitutions made at the right moment) is the
same as if it was the whole commands wich were stored in the array.
I have difficulties to be accustomed (but it's not hopeless...) to the
idea that a variable (image name if you remember) can refer to code to
be executed. And pushing things a little further the contruction of
the refering variable can itself be "variable" (i.e. can vary). That
leads to a lot of flexibility, a kind of "Programmer's Paradise" !

Jean-Marie