G'day Kaughper,
Welcome to the Monastery.
"There is no code in the button command that even mentions the items for the first Canvas."
Well, actually there is. You've given $imgA the name 'image'. In &Add_B, you've used the same Photo named 'image'.
In addition, that's potentially confused with the name of the Canvas item type:
$cvsA->create('image', ...); $cvsB->create('image', ...);
A quick fix would be to name them 'imageA' and 'imageB'. I've tested that and it works.
You have a more insidious problem waiting to happen: &Add_B relies on a variable not passed to that subroutine. This is one of the most common problems with Tk programs; fortunately, it is easily avoided. Consider changing the code after defining your XPMs to something like this (which I've successfully tested):
my %image_data_for = ( A => { name => 'ImageA', data => $LtrA, }, B => { name => 'ImageB', data => $LtrB, }, ); # Image A holder in Main Window $mw->Label(-text => 'Image A:')->pack(); my $cvsA = $mw->Canvas(-background=>'yellow', -height => 40, -width => + 40)->pack(); # Image B holder in Main Window $mw->Label(-text => 'Image B:')->pack(); my $cvsB = $mw->Canvas(-background=>'green', -height => 40, -width => +40)->pack(); # Set Image A create_canvas_image($cvsA, $image_data_for{A}); # Button to make Image B $mw->Button( -text => 'Add Letter B', -command => [\&create_canvas_image, $cvsB, $image_data_for{B}], )->pack(-pady => 5); MainLoop; sub create_canvas_image { my ($canvas, $image_data) = @_; my $img = $mw->Photo($image_data->{name}, -data => $image_data->{d +ata}); $canvas->create('image', 30, 30, -image => $img); }
Now you only have one routine (&create_canvas_image) to create all images: your example, which I'm guessing is just a proof-of-concept, only has two; but this will work for whatever number of images you want in your real-world application.
Please note that I've only shown an example for demonstrating a technique. For production-grade code, I'd be limiting the scope of most of the variables: giving them all file-scope is not a good idea as any subsequent code after their definition can change their values.
— Ken
In reply to Re: Tk::Photo XPM images being copied
by kcott
in thread Tk::Photo XPM images being copied
by Kaughper
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |