tags:

views:

254

answers:

3

I'm looking for a very simple canvas for python. What I really need is the ability to draw lines and circles, move them around / get rid of them, and scroll the canvas (so, I'm ideally drawing on an infinite canvas, and just scrolling it around). Ideally, the code would look like:

c = Canvas()
l1 = c.line((x0, y0), (x1, y1))
l2 = c.line((x2, y2), (x3, y3))
c1 = c.circle(((x0 + x1 + x2 + x3)/4, (y0 + y1 + y2 + y3)/4), 10)
c1.delete()
l1.move(5, 10)
c.scroll(5, 5)

That's just some dream code, I'm fine with some minimal boilerplate, but I really don't need anything fancy, probably the only feature I would really like would be the ability to embed in some GUI that looks good on Windows (that rules out Tkinter) and is not extremely heavyweight (that might rule out GTK/Cairo).

This is in Python 2.6. I'd be happy to give any other information

A: 

I've used PyGame with a good deal of success for such things:

http://www.pygame.org/

gahooa
I've looked at PyGame and it looks nice, but how can I move a surface around? I don't want to manually redraw hundreds of lines each time the surface moves. Also, is there any way to remove a circle or lines once you've painted it?
pavpanchekha
I created a set of "objects". Each "object" (be it a rocket, or missile, or star) had a method to draw itself. Then it is simple to only draw what is in the visible region. Each frame, I would draw on a region, and then swap that into the visible display. There is zero flickering, and you are 100% in control. It may be a bit more complex than you are looking for.
gahooa
A: 

You may want to look at this question: http://stackoverflow.com/questions/67000/fast-pixel-precision-2d-drawing-api-for-graphics-app

The first suggestion is for pyglet, http://code.google.com/p/pyglet/

James Black
A: 

I ended up using WxPython with the built-in FloatCanvas. I wouldn't really advise it for anyone else, though; it depends on NumPy, which is a very big installation, and is almost completely undocumented (reading the sources was a big part of the app I programmed). It is, however, very nice and does a lot for you.

WxWiki: http://wiki.wxpython.org/FloatCanvas

Docs: http://www.wxpython.org/docs/api/wx.lib.floatcanvas-module.html

Devel: http://trac.paulmcnett.com/floatcanvas

pavpanchekha