问题描述
这几乎是您可以在 pygame 中编写的最简单的代码.它所做的只是创建一个窗口并允许您关闭它.但是,我收到此错误pygame.error: 视频系统未初始化.我在网上搜索了这个,似乎大多数人忘记调用 pygame.init().我不确定为什么会收到此错误.
This is pretty much the simplest code you can make in pygame. All it does is create a window and allow you to close it. However, I am getting this errorpygame.error: video system not initialized. I searched online for this and it seems most people forget to call pygame.init(). I am not sure why I am getting this error.
import pygame
pygame.init()
screen = pygame.display.set_mode((900,500))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
推荐答案
只需添加 sys.exit(0)
和结尾就完成了
Just add sys.exit(0)
and the end and you are done
以下是官方文档中的一些信息
Here is some info from official document
pygame.quit() 取消初始化所有 pygame 模块 quit() -> 无取消初始化之前已初始化的所有 pygame 模块.当 Python 解释器关闭时,调用此方法无论如何,所以你的程序不应该需要它,除非它想要终止其 pygame 资源并继续.打电话是安全的多次调用此函数:重复调用无效.
注意,pygame.quit()uninitialize 所有 pygame 模块不会退出你的程序.考虑让你的程序以同样的方式结束正常的python程序会结束.
Note, that pygame.quit()uninitialize all pygame modules will not exit your program. Consider letting your program end in the same way a normal python program will end.
这是给您的示例代码.
import pygame
import sys
pygame.init()
pygame.display.set_mode((900, 500 ) )
while True :
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
这篇关于pygame.error: 视频系统未初始化.pygame.init() 已经调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!