2d graphics - what module to use?

Lorenzo Gatti gatti at dsdata.it
Fri Jul 25 04:20:47 EDT 2008


On 25 Lug, 08:13, Pierre Dagenais <pierre.dagen... at ncf.ca> wrote:
> What is the easiest way to draw to a window? I'd like to draw something
>   like sine waves from a mathematical equation.
> Newbie to python.

What you are really asking for is what GUI library you should use;
every one allows you to draw freely. What do you need to do besides
drawing sine waves? You should look at your full range of options;
http://wiki.python.org/moin/GuiProgramming is a good starting point.

The "easiest" way to draw might be with those toolkits that offer
primarily a canvas to draw on rather than composable widgets. For
example, Pyglet (http://pyglet.org/) offers OpenGL contexts with
sensible defaults and unobtrusive automation:

from pyglet import *
from pyglet.gl import *
import math
win = window.Window(width=700, height=700, caption="sine wave demo",
resizable=True)
frequency,phase,amplitude=0.1,0.0,0.9
@win.event
def on_draw():
    half_height=win.height*0.5
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(0.9, 1.0, 0.8)
    glBegin(GL_LINE_STRIP)
    for x in xrange(0,win.width):
        y=half_height*(1.0+amplitude*math.sin(x*frequency+phase))
        glVertex2f(x,y)
    glEnd()
app.run()



Regards,

Lorenzo Gatti



More information about the Python-list mailing list