I'm working on a program for school and am required to use a pygame window to display cumulative totals for my program (it's for ordering coffee). Here is what the code looks like for the part where the window opens:
if totalconf == 'y':
# if user wishes to view cumulative totals
pygame.init()
background_colour = (231,247,146)
(width, height) = (1500, 900)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Running Totals')
# initialise new window with yellow background and title
myFont = pygame.font.SysFont("arial", 40)
myFont2 = pygame.font.SysFont("arial", 20)
# initialise small and large fonts
text = myFont.render("In store totals:", True, (0, 0, 0))
text3 = myFont.render("Takeaway totals:", True, (0, 0, 0))
text4 = myFont.render("In store earnings:", True, (0, 0, 0))
text5 = myFont.render("Takeaway earnings:", True, (0, 0, 0))
text6 = myFont.render(f"Total discounts: ${total_discounts}", True, (0, 0, 0))
text7 = myFont.render(f"Total gst: ${total_gst}", True, (0, 0, 0))
text8 = myFont.render(f"Total surcharges: ${total_takeawaycharges}", True, (0, 0, 0))
# initialise headings and totals to display on screen
screen.fill(background_colour)
# fill screen with background color
pygame.display.flip()
# update screen
running = True
# run window
while running:
# while window is running
screen.blit(text, (20, 20))
screen.blit(text3, (300, 20))
screen.blit(text4, (600, 20))
screen.blit(text5, (900, 20))
screen.blit(text6, (20, 700))
screen.blit(text7, (20, 740))
screen.blit(text8, (20, 780))
# project all text onto screen
pygame.display.update()
# update screen
initial = 70
# set initial as 70 down
for item, values in totalsdict.items():
# for values stored in totals dict
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
# print out item and equivalent total in small font
screen.blit(text2, (20, initial))
# display text on screen
pygame.display.update()
# update screen
initial += 40
# increase inital by 40 so values print vertically down
initial = 70
# set initial as 70 down
for item, values in totalsdict.items():
# for values stored in totals dict
text2 = myFont2.render(f'{item.title()}: {values[1]}', True, (0,0,0))
# print out item and equivalent total in small font
screen.blit(text2, (300, initial))
# display text on screen
pygame.display.update()
# update screen
initial += 40
# increase inital by 40 so values print vertically down
initial = 70
# set initial as 70 down
for item, values in totalsdict.items():
# for values stored in totals dict
text2 = myFont2.render(f'{item.title()}: {values[2]}', True, (0,0,0))
# print out item and equivalent total in small font
screen.blit(text2, (600, initial))
# display text on screen
pygame.display.update()
# update screen
initial += 40
# increase inital by 40 so values print vertically down
initial = 70
# set initial as 70 down
for item, values in totalsdict.items():
# for values stored in totals dict
text2 = myFont2.render(f'{item.title()}: {values[3]}', True, (0,0,0))
# print out item and equivalent total in small font
screen.blit(text2, (900, initial))
# display text on screen
pygame.display.update()
# update screen
initial += 40
# increase inital by 40 so values print vertically down
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if running == False:
pygame.quit()
# code to stop running if 'X' is pressed
If the user wants to see the cumulative totals, they input 'y' for totalconf. This bit of code sits in a continuous while loop which takes orders until a blank is entered. The first time the program runs this works great and opens and closes without stopping the program. However, the second time, if I wish to see the updated cumulative totals and open the pygame window, I get a message which says "Backend terminated or disconnected.Windows fatal exception: access violation". This is a really long program but I hope the information I have given is enough.
So in summary, I need to be able to open and close the window every time the loop runs without getting the message above.