Recently I started using my first attempt at Python multithreading. The purpose of the code is to run multiple WHILE loops at the same time, one of which searches for the change of a variable to TRUE caused by input picked up by the other. However, when i ran the program listed below, it gave me an error:

import thread, pygame, sys
def backMain(threadName, fps):
     ##Begins processing of essential info
     pygame.init()
     DISPLAY = pygame.display.set_mode((1023, 647))
     WHITE = (255, 255, 255)
     DISPLAY.fill(WHITE)
     clock = pygame.time.Clock()
     active = True
     started = False
     mainscreendisplayimg = pygmae.image.load('mainScreen.png')
     def mainscreen(x, y):
          DISPLAY.blit(mainscreendisplayimg, (x,y))
     mainscreen(1, 1)
     while active:
          for event in pygame.event.get():
               if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
         elif event.type == pygame.KEYDOWN:
              if event.key == pygame.K_p:
                   if started == False:
                        started = True
                        print 'Done'
        pygame.display.update()
        clock.tick(fps)

def forMain(threadname, fps):
     active = true
     while active:
          if started == True:
               print 'Starting Main Program'
          clock.tick(fps)

try:
    thread.start_new_thread( backMain, ("backwardMain", 15))
    thread.start_new_thread( forwardMain, ("forwardMain", 15))
except:
     print "Unable to start threads"

This should display 'mainScreen.png' on the screen with a white background, and when I press p, should start printing 'Good'. When I run it, however, the Pygame screen will remain completely black and I will get an error stating:

Unhandled exception in thread started by <function forwardMain at 0x02D38470>

Any help would be appreciated

The first rule of thread programming in python is not to use the thread module. Use the threading module instead. The reason is that the thread module is too low level for your needs.

Read Click Here first and update your code to use threading.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.