[Edu-sig] Introducing Python to Engineering Students

David MacQuigg macquigg at ece.arizona.edu
Thu Mar 13 23:46:28 CET 2008

At 01:16 PM 3/13/2008 -0700, Warren wrote:

>There is a known problem with Pygame and IDLE.  (Something about IDLE not running the program in a separate process.)  If you just run the program form a command window, or use a different editor (like SPE) you wouldn't see that problem about closing the window.

Well, it's probably not the TKinter "dueling event loops" problem. (PyGame doesn't use TKinter for its own event loop.)  But I do see an FAQ at http://www.pygame.org/wiki/search.php?query=IDLE and the proposed solution is to make sure there is always a call to pygame.quit().  The example in the FAQ doesn't work, but I see what they are trying to do.  Here is how I would write the final loop:  
while True: 
    event = pygame.event.wait() 
    if event.type == pygame.QUIT: 
    if event.type == pygame.MOUSEBUTTONDOWN: 
        print "Enter new data"

Now there is no problem running from IDLE.

>Changing the program so it updates as the calculation is ongoing is certainly possible.

I'll play around with this some more.  I'm very pleased with the documentation on PyGame.  Without knowing anything about the program, I was able to find the functions and event types used in the snippet above.

Many thanks for your help.

-- Dave

>----- Original Message ----
>From: David MacQuigg <macquigg at ece.arizona.edu>
>To: Warren Sande <warren.sande at rogers.com>
>Sent: Thursday, March 13, 2008 2:51:00 PM
>Subject: Re: [Edu-sig] Introducing Python to Engineering Students
>At 08:19 PM 3/12/2008 -0700, Warren Sande wrote:
>>By the way, here is a non-OO version of a the fractals program that uses Pygame to display the output.
>Very nice!  I installed PyGame under Python25/Lib/site-packages, using the Windows installer from <http://www.pygame.org/install.html>http://www.pygame.org/install.html, and ran the script below from my favorite IDE (IDLE).  I wish Unix installs would go so smoothly.
>The only problem I noticed is that the CPU runs 100% and doesn't stop if you forget to click the X on the PyGame window.  When I do click the X, the event loop stops, but he window remains.  If I click the X again, I get Microsoft's "program is not responding" dialog, and its "please tell Microsoft" follow-on.  There must be a better way to close this window.
>Also, it would be nice if we could show the window as it is being painted, and allow students to interrupt a long computation and try different coordinates.
>-- Dave
>># Simple fractal program using Pygame to display results
>># (Based on Kirby Urner's OO version)
>>import pygame, sys
>>palette = [(0,0,0)]
>>def mkpalette():
>>    global palette
>>    for i in range(0,255):
>>        palette.append((i*5%200 + 20, i*7%200 + 20, i*11%200 + 20))
>>    return palette
>>def compute_fractal(n, uleft_x, uleft_y, lright_x, lright_y):
>>    global pixels
>>    xwidth = lright_x - uleft_x
>>    ywidth = uleft_y  - lright_y 
>>    pixels = []
>>    for x in range (500):
>>        pixel_row = []
>>        for y in range (500):
>>            percentx = x/500.0
>>            percenty = y/500.0
>>            xp = uleft_x + percentx * xwidth
>>            yp = uleft_y - percenty * ywidth
>>            z = complex(xp,yp)
>>            o = complex(0,0)
>>            dotcolor = 0
>>            for trials in range(n):
>>                if abs(o) <= 2.0:
>>                    o = o**2 + z
>>                else:
>>                    dotcolor = trials
>>                    break
>>            pixel_row.append(palette[dotcolor])
>>        pixels.append(pixel_row)
>>pixels = []
>>print "computing fractal..."
>>compute_fractal(64, -2, 1.25, 0.5, -1.25)
>>print "done."
>>screen = pygame.display.set_mode((500,500))
>>f_surf = pygame.Surface((500, 500))
>>for x in range(500):
>>    for y in range(500):
>>        f_surf.set_at((x, y), pixels[x][y])
>>screen.blit(f_surf, [0,0,500,500])
>>while True:
>>    for event in pygame.event.get():
>>        if event.type == pygame.QUIT:
>>            sys.exit()
>>----- Original Message ----
>>From: Warren Sande <<mailto:warren.sande at rogers.com>warren.sande at rogers.com>
>>To: <mailto:edu-sig at python.org>edu-sig at python.org
>>Sent: Tuesday, March 11, 2008 12:20:02 AM
>>Subject: Re: [Edu-sig] Introducing Python to Engineering Students
>>For output graphics, you might want to have a look at <<http://www.pygame.org/>http://www.pygame.org/>Pygame.  It is a wrapper for the SDL library.  It has functionality for creating graphics windows, drawing, sprites, etc.  But what might be of interest for you is the simple set_at(x,y) method, to set the color of individual pixels in a window.
>>I have found the Pygame documentation to be pretty good.
>>Here is a simple example of plotting a sinewave using set_at()
>>import pygame, sys, math
>>screen = pygame.display.set_mode([640,480])
>>for x in range(0, 640):
>>    y = int(math.sin(x/640.0 * 4 * math.pi) * 200 + 240)
>>    screen.set_at([x, y],[255,0,0])
>>while True:
>>    for event in pygame.event.get():
>>        if event.type == pygame.QUIT:
>>            sys.exit()
>>Warren Sande

More information about the Edu-sig mailing list