michaelendow
michaelendow

Reputation: 31

Segmentation Fault with opening and closing Pygame WIndows

I'm working on a project where I am trying to alternate between the camera preview on a picamera, and some text on screen using pygame windows. I have gotten to the point where I can open the picamera, then some text, then the picamera again, but when I try to open a pygame window for more text, I get a segmentation fault.

I think the main problem is quitting the pygame window, without quitting the other things I need to open up another pygame window. The commands like sys.exit, and pygame.quit seem to quit things to much. I've tried alternatives like putting the text into a while loop and then making the loop false at the end so that it closes the window without an actual quit command, but that didn't seem to close anything really. The code works perfectly up until the second time I try to initialize pygame. That's when it gives me the segmentation fault and opens a new window in my python idle with a whole bunch of other code that I didn't write.

pygame.init()

white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption('   ')

camera()

font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('You', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white)
display_surface.blit(text, textRect)
for event in pygame.event.get():
     if event.type == pygame.QUIT:
         pygame.quit()
         quit()
     pygame.display.update()
     time.sleep(1)
     pygame.quit()

camera()

pygame.init()

white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption('   ')
font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('test', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white) 
display_surface.blit(text, textRect)
for event in pygame.event.get():
   if event.type == pygame.QUIT:
        pygame.quit()
         quit()
pygame.display.update()
time.sleep(1)

What I would like to be able to do is switch between picamera and text several more times, so if I can figure out how to fix it this one time, then I just have to copy and paste the code a bit more to get the next iterations. I'm brand new to coding.

Upvotes: 1

Views: 789

Answers (1)

michaelendow
michaelendow

Reputation: 31

Ok. so I made a silly mistake. The problem is, for some reason, initializing pygame twice; which I still don't understand since pygame.quit() should have quit it I think. But I just removed the second pygame.init() and replaced the first pygame.quit() with pygame.display.quit() instead.

Upvotes: 2

Related Questions