rectangles, or cavases, or ... ?

John McMonagle jmcmonagle at NO.SPAM.velseis.com.au
Mon Apr 20 19:43:46 EDT 2009


Alan G Isaac wrote:
> I need to display many (e.e., 2000) small squares whose colors are udpated
> each time a computation is complete.
> 
> One approach is to put rectangles on a single canvas.
> Another approach is to put many canvases in a single frame.
> Another approach is to create an image each iteration,
> which is placed on a canvas.
> Other?
> 
> Are there obvious considerations in the choice?
> (Right now I do not need to interact with the squares,
> but in the future I may need to.)
> 
> Thanks,
> Alan Isaac

Approach 1: put many rectangles on a single canvas

This is the most flexible approach.  It allows you to take advantage of
Tkinter canvas tag bindings to interact with individual or groups of
canvas items.   It also allows you to use some pretty handy find methods
for the canvas items (find_closest, find_withtag, etc).  You can
configure properties of the item, such as fill colour, without needing
to redraw all the items.

The downside of this approach is, because each rectangle you draw is a
canvas item, it is also an object.  The implications of this is that
your memory usage increases with the more items you draw.



Approach 2: put many canvasses on a frame

This has no advantages.  In fact it has no good points at all.

Memory usage would quickly increase.  You would have to use the place
geometry manager to put the canvasses exactly where you want in the
frame. You would have to write a bunch of custom functions to interact
with all the canvasses.


Approach 3: create an image and draw on a canvas

This approach may be the solution if the number of canvas items becomes
too memory intensive.  You can construct the image object using an image
library like PIL or aggdraw and place it on a Canvas.

However, every time you need to update the image you need to redraw the
whole image.

My advice....

as long as your system can handle the memory usage, I would opt for the
"draw many rectangles on a single canvas" approach.


Regards,

John



More information about the Python-list mailing list