rectangles, or cavases, or ... ?

A

Alan G Isaac

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
 
F

FogleBird

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

First of all, what GUI toolkit are you using?

Secondly, in wxPython I would subclass wx.Panel and handle the
EVT_PAINT event to just draw the rectangles. Generally I render to an
off-screen DC and just blit that in the paint event.

What kind of frame rate do you need?
 
A

Alan G Isaac

You should have said, but I'll guess you are using Tkinter.

Yes, I should have, sorry. You are right, I'm using Tkinter.

I'd put the rectangles on a canvas, myself.

That's my current choice, but I'm not seeing into
what the trade-offs are. That's my question: what's
the basis of your choice?

Thanks,
Alan

PS Am I right that Tkinter does not offer the ability to
draw many rectangles in one go (along the lines of the
PostScript rectfill operator)?
 
J

John McMonagle

Alan said:
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
 
A

Alan G 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.


Great summary, and you clued me into a bunch of
methods I had not explored.
Thanks!
Alan
 
A

Alan G Isaac

First of all, what GUI toolkit are you using?

Tkinter. (Sorry; I meant to include that in the subject line.


Secondly, in wxPython I would subclass wx.Panel and handle the
EVT_PAINT event to just draw the rectangles. Generally I render to an
off-screen DC and just blit that in the paint event.

I'm guessing that approach will be fastest.

What kind of frame rate do you need?

20fps or less.
(Actually, at the moment, much less.)

Thanks,
Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top