Pygame错误:视频系统未初始化

14 投票
1 回答
20003 浏览
提问于 2025-04-16 22:34

我之前用过Pygame和Python 2.7,但最近我“升级”到了Python 3.2。我下载并安装了最新版本的Pygame,听说这个版本可以和我的Python配合使用。不过,我在运行一段本该很简单的代码时,遇到了一个让人很沮丧的错误。代码是:

import pygame, random

title = "Hello!"
width = 640
height = 400
pygame.init()
screen = pygame.display.set_mode((width, height))
running = True
clock = pygame.time.Clock()
pygame.display.set_caption(title)

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.quit():
            running = False
        else:
            print(event.type)
    clock.tick(240)
pygame.quit()

每次我运行这段代码,都会出现:

17
1
4
Traceback (most recent call last):
  File "C:/Users/David/Desktop/hjdfhksdf.py", line 15, in <module>
    for event in pygame.event.get():
pygame.error: video system not initialized

为什么我会遇到这个错误呢?

1 个回答

16
if event.type == pygame.quit():

在上面那行代码中,你调用了 pygame.quit() 这个函数,但你其实想要的是常量 pygame.QUIT。调用 pygame.quit() 后,pygame 就不再初始化了,这就是你出现错误的原因。

所以,把那行代码改成:

if event.type == pygame.QUIT: # Note the capitalization

就能解决你的问题。

需要注意的是, pygame.quit() 并不会退出程序。

撰写回答