in reply to Duplicate views to a single canvas using Perl TK

What you want is very hard to do, especially for a beginner. If you try and copy or clone the canvas, it will just overwrite the first at the exact locations on the screen. You are looking for some sort of deep object copying and blessing of the new object, and it dosn't work well with Tk. You might have some luck if you subclassed your Canvas, and used Clone (or something similar) on it. But, it will probably be buggy at a very deep level, which would drive a beginner crazy. Your best bet, is to do either one of 2 things.

1. Every time you add or delete and object in 1 canvas, duplicate that move in the other, or maintain a log, and use the log to modify the other Canvas like an undo/redo script.

2. Stick to a single Canvas, and when you create 1 item in the Canvas, create another at an offset, to appear like in a second Canvas. Maybe have a key to prevent the cloning, or to erase from only a single canvas.

When you get into involved Canvas apps, you need to use the power of "tagging items". With creative-descriptive tagging, you can find and manipulate items easily.


I'm not really a human, but I play one on earth. Cogito ergo sum a bum
  • Comment on Re: Duplicate views to a single canvas using Perl TK

Replies are listed 'Best First'.
Re^2: Duplicate views to a single canvas using Perl TK
by Helter (Chaplain) on May 03, 2008 at 17:20 UTC
    Unfortunately the drawing to the canvas is a third party sort-of code that we don't want to muck too deeply with.

    It seems like there isn't a real way to create 2 views of the same data, so our best bet is to just create 2 of the canvas. This will give us the most functionality, and we have some preliminary tests that say the impact on memory footprint isn't bad enough for this not to work.

    Thanks for the suggestions, I'll keep them in mind for future projects working with TK.

    If someone does have a way to do it I'd still love to hear about it, but for now I think we have a method that might work.
      This is probably your best bet.

      As you noticed, having multiple canvases does not impact memory usage that much since much of the memory is shared between multiple instantiations of the same widget.

      The only other optimization I can think of is that you don't need to invoke the third party code twice. What you can probably do is invoke it once to get the object drawn on one canvas, then grab the coords of the new object (using the coords() method), and draw it on the second canvas. Probably a bit faster.

      HTH.