From: Gerald W. Lester on
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.Lester(a)kng-consulting.net |
+------------------------------------------------------------------------+
From: Gerald W. Lester on
jmc wrote:
> On 27 juil, 05:23, "Gerald W. Lester" <Gerald.Les...(a)KnG-
> Consulting.net> wrote:
>> ...
>>
>> 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}"

From your comments, I see that you did not try to actually run the script!

>
> 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

Not exactly, but for this line the effect is the same. What really happens is:
1) Break the command (i.e. line) into words, resulting in: '$c1$c2$c3',
'$to' and '$value'
2) Process substitution on the first word -> 'set'
3) Process substitution on the second word -> 'X'
4) Process substitution on the third word -> '45'
5) Invoke the command specified by the first word (i.e. set) passing the
second and third words to it as its arguments (e.g. X and 45).


> 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"

Wrong!!! Your a/b idea is not how Tcl works, reread Tcl's syntax rules --
this may seem like nit picking but it is a really fundamental and important
point.

Try again, process according to the Tcl syntax rules (use the example from
above as a start -- it only uses a couple of the rules, this example uses
exactly one more rule then above).

Post again when you understand what the second example does and why.

--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald.Lester(a)kng-consulting.net |
+------------------------------------------------------------------------+
From: Spam on
On Mon, 26 Jul 2010, jmc wrote:

> Thanks Rob. Very interesting. I didn't realize it was possible to
> store a command in an array. Cool !
>
> Jean-Marie
>

Gerald is correct when he indicates that I'm *NOT* storing a command, but
rather a reference to a widget upon which I can operate.

Note that when you create a widget, it typically returns the path to the
newly created widget. It is that reference which you need to refer when
referencing images (or widgets) for future use.

Similar techniques can be achieved for general purpose widgets which need
to be referenced repeatedly, and they can be cached in arrays, lists,
dicts or whatever container is convenient for you ... I simply prefer to
use procedure and "keys" to reference the widgets which live a long time
rather than having to remember paths which quickly become long, and
ultimately meaningless, as the container hierarchy is only important in
the initial screen layout ... not during the execution of the application
proper.

HTH,
Rob Sciuk