Python program won't respond until keyboard interrupt
Question: I'm trying to draw an image with PyGame, and I copied this code from Adafruit.
It's strange, seems nothing displays on the screen until I Ctrl-C, at which
time it shows the image. Then the image stays on the screen until I press
Ctrl-C again. I then get the following message:

> Traceback (most recent call last):  
>  File "RaspiDisplay.py", line 60, in  
>  time.sleep(10)  
>  KeyboardInterrupt

What's going on? By the way, I'm running this via ssh on a raspberry pi, with
Display set to 0 (my TV) If I put a print statement in **init** , that also
doesn't print until I press Ctrl-C.

    
    
    import os
    import pygame
    import time
    import random
    
    class pyscope :
        screen = None;
    
        def __init__(self):
            "Ininitializes a new pygame screen using the framebuffer"
            # Based on "Python GUI in Linux frame buffer"
            # http://www.karoltomala.com/blog/?p=679
            disp_no = os.getenv("DISPLAY")
            if disp_no:
                print "I'm running under X display = {0}".format(disp_no)
    
            # Check which frame buffer drivers are available
            # Start with fbcon since directfb hangs with composite output
            drivers = ['fbcon', 'directfb', 'svgalib']
            found = False
            for driver in drivers:
                # Make sure that SDL_VIDEODRIVER is set
                if not os.getenv('SDL_VIDEODRIVER'):
                    os.putenv('SDL_VIDEODRIVER', driver)
                try:
                    pygame.display.init()
                except pygame.error:
                    print 'Driver: {0} failed.'.format(driver)
                    continue
                found = True
                break
    
            if not found:
                raise Exception('No suitable video driver found!')
    
            size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
            print "Framebuffer size: %d x %d" % (size[0], size[1])
            self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
            # Clear the screen to start
            self.screen.fill((0, 0, 0))        
            # Initialise font support
            pygame.font.init()
            # Render the screen
            pygame.display.update()
    
        def __del__(self):
            "Destructor to make sure pygame shuts down, etc."
    
        def test(self):
            # Fill the screen with red (255, 0, 0)
            red = (255, 0, 0)
            self.screen.fill(red)
            # Update the display
            pygame.display.update()
    
    # Create an instance of the PyScope class
    scope = pyscope()
    scope.test()
    time.sleep(10)
    


Answer: You're not running an event loop anywhere. Instead, you're just initializing
everything, and then going to sleep for 10 seconds. During that 10 seconds,
your code is doing nothing, because that's what you told it to do. That means
no updating the screen, responding to mouse clicks, or anything else.

There are a few different ways to drive pygame, but the simplest is something
like this:

    
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            # any other event handling you need
        # all the idle-time stuff you want to do each frame
        # usually ending with pygame.display.update() or .flip()
    

See the [tutorial](http://www.pygame.org/docs/tut/intro/intro.html) for more
information.

* * *

As a side note, your initialization code has a bunch of problems. You iterate
through three drivers, but you only set `SDL_VIDEODRIVER` once, so you're just
trying `'fbcon'` three times in a row. Also, you've got code to detect the X
display, but you don't allow pygame/SDL to use X, so… whatever you were trying
to do there, you're not doing it. Finally, you don't need a `found` flag in
Python for loops; just use an `else` clause.

