python - 打开和关闭 Pygame WIndows 时出现段错误

标签 python raspberry-pi pygame

我正在开发一个项目,尝试在 picamera 上的相机预览和使用 pygame 窗口的屏幕上的一些文本之间切换。我已经可以打开 picamera,然后打开一些文本,然后再次打开 picamera,但是当我尝试打开 pygame 窗口以获取更多文本时,出现段错误。

我认为主要问题是退出 pygame 窗口,而不退出我需要打开另一个 pygame 窗口的其他内容。像 sys.exit 和 pygame.quit 这样的命令似乎可以退出很多东西。我尝试过其他选择,例如将文本放入 while 循环中,然后在最后使循环为 false,以便在没有实际退出命令的情况下关闭窗口,但这似乎并没有真正关闭任何内容。该代码一直运行良好,直到我第二次尝试初始化 pygame。就在那时,它给了我段错误,并在我的 python 空闲状态下打开了一个新窗口,其中包含一大堆我没有编写的其他代码。

pygame.init()

white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption('   ')

camera()

font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('You', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white)
display_surface.blit(text, textRect)
for event in pygame.event.get():
     if event.type == pygame.QUIT:
         pygame.quit()
         quit()
     pygame.display.update()
     time.sleep(1)
     pygame.quit()

camera()

pygame.init()

white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption('   ')
font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('test', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white) 
display_surface.blit(text, textRect)
for event in pygame.event.get():
   if event.type == pygame.QUIT:
        pygame.quit()
         quit()
pygame.display.update()
time.sleep(1)

我希望能够在 picamera 和文本之间多切换几次,所以如果我能弄清楚如何修复它这一次,那么我只需要复制并粘贴代码更多一点得到下一个迭代。我是编码新手。

最佳答案

好的。所以我犯了一个愚蠢的错误。问题是,由于某种原因,初始化 pygame 两次;我仍然不明白,因为我认为 pygame.quit() 应该退出它。但我只是删除了第二个 pygame.init() 并将第一个 pygame.quit() 替换为 pygame.display.quit()

关于python - 打开和关闭 Pygame WIndows 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58617733/

相关文章:

python - 如何在linux命令行上检查pdf打印是否完成

python - 如何在 matplotlib 饼图中设置楔形边框?

python - 如何生成所有数字都是偶数的数字列表?

java - 在 Raspberry Pi 上构建 Gradle 导致 "Failed to create parent directory"

python - 使用 pygame 显示 unicode 符号

python - 使用 lxml/ElementTree 获取非连续文本

树莓派上的Java启动慢?

linux - SCP 命令有效,但我找不到下载的文件

python - 检查 Pygame.Key 中的 Capslock

python - Pygame 缩放健康栏