在 Python 中使用空格而不是制表符时出现奇怪的错误

Weird error when using spaces instead of tab in Python

我正在使用 pygame 开发一个简单的 GUI。在某些时候,由于制表符和空格的混合,我受够了不断出现的缩进错误,所以我决定使用 vi 将所有制表符替换为 4 个空格。在那之后我得到一些关于 pygame.font.SysFont 没有被初始化的错误,即使它是在之前。 自然地,我认为这与我只是将制表符更改为空格有关,因此我确保所有内容都正确缩进等。我注释掉 95% 的代码并开始与旧版本的代码进行比较,从旧版本复制行代码到新的。因为它们看起来是相同的(使用 cat -A file.py 来比较不可见的字符)。

我终于发现这是罪魁祸首: trouble-maker

这是两个文件之间唯一不同的地方(不在三引号中)。将此行更改为具有选项卡确实可以解决问题。 所以,我想问题解决了。

我的问题是: 这怎么可能?空格不应该比制表符更不容易出错吗?

代码如下所示:

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)

我在这里错过了什么?为什么 pygame.quit() 前面的制表符可以工作,但没有 4 个空格它会给出 "pygame.error: font not initialized"

编辑: 这是回溯

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

另请注意,不需要执行 pygame.font.init(),请参阅 https://www.pygame.org/docs/tut/ImportInit.html

pygame.font.init() 需要使用字体

如果你把 pygame.quit() 放在没有标签的地方,那么你就会得到类似这样的东西

(在 init()quit() 之间有 class 并不重要 - 它的工作方式相同)

import pygame

pygame.init()
pygame.quit()

class ...

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)

所以您初始化 pygame 并立即退出 pygame - 当您尝试使用 pygame 的某些元素(如 font)时出现错误.

您应该在 if __name__ == "__main__": 内的正确位置执行此操作。

import pygame

class ...

if __name__ == "__main__":

    # starts program
    pygame.init()

    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)

    # ... other code ...

    # ends program
    pygame.quit()

顺便说一句: 这可能更有用(而不是 scr_widthscr_height

self.screen_rect = self.screen.get_rect()

因为您总是可以获得 self.screen_rect.widthself.screen_rect.height 以及 self.screen_rect.center(使元素在屏幕上居中)或其他。