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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.