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

时间:2017-01-19 11:43:31

标签: python tabs pygame spaces

我正在使用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

2 个答案:

答案 0 :(得分:2)

如果您将pygame.quit()放在没有标签的情况下,那么您就有了类似的内容

classinit()之间quit()并不重要 - 它的工作方式相同)

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)时,您会收到错误。< / p>

您应该在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()

BTW:这可能更有用(而不是scr_widthscr_height

self.screen_rect = self.screen.get_rect()

因为您始终可以获得self.screen_rect.widthself.screen_rect.heightself.screen_rect.center(屏幕上的中心元素)或其他内容。

答案 1 :(得分:-1)

使用pygame.font.init()来使用字体