I am working on a simple GUI using pygame. At some point I was fed up with the constant indentation errors due to tabs and spaces mixing, so I decided to use vi to replace all tabs with 4 spaces. After that I get some error about pygame.font.SysFont not being initialized, even though it was before. Naturally I figure this has something to do with me just changing the tabs to spaces so I make sure everything is indented correctly etc. I comment out 95% of the code and start comparing with an old version of the code, copying lines from the old code to the new one. Since they seem to be identical (using cat -A file.py to compare also the invisible characters).
I finally find out that this is the culprit: trouble-maker
This is the only thing (that is not in triple quotes) that is different between both files. Changing this line to have a tab does indeed fix the problem. So, problem solved, I guess.
My question is: How is this possible? Shouldn't the spaces be less error-prone than the tab?
The code looks like this:
import pygame
pygame.init()
class GameMenu():
def __init__(self, screen, items, bg_color=(237,237,223), font="Verdana", font_size=30,
font_color=(237, 28, 36)):
self.screen = screen
self.scr_width = self.screen.get_rect().width
self.scr_height = self.screen.get_rect().height
self.bg_color = bg_color
self.clock = pygame.time.Clock()
self.items = items
self.font = pygame.font.SysFont(font, font_size)
self.font_color = font_color
"""
rest of the code commented out
"""
pygame.quit()
if __name__ == "__main__":
screensize = 0
screen = pygame.display.set_mode((640, 480), screensize, 32)
menu_items = ('1', '2', '3', '4', '5')
pygame.display.set_caption('numbers')
pygame.mouse.set_visible(True)
gm = GameMenu(screen, menu_items)
What am I missing here? Why would a tab in front pygame.quit() work, but without 4 spaces it gives "pygame.error: font not initialized"
EDIT: Here is the traceback
Traceback (most recent call last):
File "testMenu.py", line 168, in <module>
gm = GameMenu(screen, menu_items)
File "testMenu.py", line 31, in __init__
self.font = pygame.font.SysFont(font, font_size)
File "/usr/lib/python2.7/dist-packages/pygame/sysfont.py",
line 614, in SysFont
return constructor(fontname, size, set_bold,
set_italic)
File
"/usr/lib/python2.7/dist-packages/pygame/sysfont.py", line 537, in font_constructor
font = pygame.font.Font(fontpath, size)
pygame.error: font not initialized
Please also note that it is not required to do pygame.font.init(), see https://www.pygame.org/docs/tut/ImportInit.html