You could just draw a suitable image that displays the grid of "cells" properly (draw a bunch of rectangles all having the desired width and height, with different colors depending on their initial states), and then use arithmetic on the mouse position to identify which cell was under the mouse when there was a click (or, more carefully, to determine whether the button-down and button-up events were within the same cell, and which cell that was).

You wouldn't need to use tags at all, if the column and row dimensions are fixed (or at least, a fixed proportion relative to the actual size of the canvas). Just grab the mouse coordinates on the button-down and button-up events, and quantize these wrt the cell width and height to determine which row and column holds the cell. Keep a separate 2-D data array to hold the current state of each cell, and as you update that array according to mouse actions, simply redraw the given rectangle with some suitable color to illustrate the change of state on that cell.

If you prefer to use images to illustrate the various states of cells (could get a bit noisy, visually, but you can be the judge of that), then presumably the set of states/images is fairly small, and copying a small rectangular image to a given x,y position is not that different from drawing a solid color rectangle at that position.

update: this made me think about "well, what are tags good for, then?" I think what they are good for are cases where the canvas may contain overlapping objects, and when someone clicks a position occupied by two or more objects, you get to see how many objects are involved, and which ones. But that doesn't seem to apply in your case, so you shouldn't bother with tags.


In reply to Re: Perl Tk Canvas and buckets of items by graff
in thread Perl Tk Canvas and buckets of items by herveus

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.