TypeError: 'pygame.Surface' object is not subscriptable Pygame false error

Viewed 9

I'm trying to make a progress bar with pygame by writing the following code:

import pygame
pygame.init()
window = pygame.display.set_mode((pygame.display.Info().current_w, pygame.display.Info().current_h))
text = ["Reading data ...", "Setting configs ...", "Loading files ...", "Download complete"]
textIndex = 0
font = pygame.font.SysFont("Arial", 30)

y2 = 400

while True:
    for event in pygame.event.get( ):
        if event.type == pygame.QUIT:
            quit()
    
    text = font.render(text[textIndex], False, (0, 255, 0))
    length = 30 * len(text[textIndex])
    x2 = (pygame.display.Info().current_w / 2) - length / 2
    window.blit(text, (x2, y2))

    pygame.display.flip()

However, it gives me this pygame error message:

Traceback (most recent call last):
  File "", line 16, in <module>
    length = 30 * len(text[textIndex])
TypeError: 'pygame.Surface' object is not subscriptable

This message comes with a line that has nothing to do with pygame, can anyone tell me how to correct this?

1 Answers

You cannot use the same variable name for the list of strings and the rendered text Surface. Change the name:

text = font.render(text[textIndex], False, (0, 255, 0))

text_surf = font.render(text[textIndex], False, (0, 255, 0))

window.blit(text, (x2, y2))

window.blit(text_surf, (x2, y2))
Related