Python pygame.error: 视频系统未初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26767591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pygame.error: video system not initialized
提问by Badfitz66
so I get this error when I try to run my pygame code: pygame.error: video system not initialized
所以当我尝试运行我的 pygame 代码时出现此错误:pygame.error: video system not initialized
i specify where the wing IDE tells me it is in the code below
我在下面的代码中指定翼 IDE 告诉我它的位置
import os
import sys
import math
import pygame
import pygame.mixer
from pygame.locals import *
black = 0,0,0
white = 255,255,255
red = 255,0,0
green = 0,255,0
blue = 0,0,255
screen = screen_width, screen_height = 600, 400
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
fps_cap = 120
running = True
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.display.flip()
pygame.quit()
sys.exit
#!/usr/bin/env python
采纳答案by abarnert
You haven't called pygame.init()anywhere.
你没有给pygame.init()任何地方打电话。
See the basic Intro tutorial, or the specific Import and Initializetutorial, which explains:
Before you can do much with pygame, you will need to initialize it. The most common way to do this is just make one call.
在你可以用 pygame 做很多事情之前,你需要初始化它。最常见的方法是拨打一个电话。
pygame.init()
This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. For example to only initialize the font module you would just call.
这将尝试为您初始化所有 pygame 模块。并非所有 pygame 模块都需要初始化,但这会自动初始化需要初始化的模块。您还可以轻松地手动初始化每个 pygame 模块。例如,仅初始化您将调用的字体模块。
In your particular case, it's probably pygame.displaythat's complaining that you called either its set_captionor its flipwithout calling its initfirst. But really, as the tutorial says, it's better to just initeverything at the top than to try to figure out exactly what needs to be initialized when.
在您的特定情况下,可能pygame.display是抱怨您set_caption在flip没有先调用它的情况下调用了它或它init。但实际上,正如教程所说,最好将init所有内容都放在顶部,而不是试图弄清楚什么时候需要初始化。
回答by Julien Dubois
You get an error because you try to set the window title (with set_caption()) but you haven't created a pygame window, so your screenvariable is just a tuple containing the size of your future window.
您会收到错误消息,因为您尝试设置窗口标题(使用set_caption())但您还没有创建 pygame 窗口,因此您的screen变量只是一个包含未来窗口大小的元组。
To create a pygame window, you have to call pygame.display.set_mode(windowSize).
要创建一个 pygame 窗口,您必须调用pygame.display.set_mode(windowSize).
Good luck :)
祝你好运 :)
回答by Tim Pozza
I had this issue recently, and I discovered a strange and unusual bug in the code that I'd written -- only after reading it and re-reading it a dozen times over a 10 minute stretch, trying to launch the file (which looked perfect) a dozen times.
我最近遇到了这个问题,我在我编写的代码中发现了一个奇怪且不寻常的错误——只有在阅读它并在 10 分钟内重新阅读了十几次之后,才尝试启动文件(看起来像完美)十几次。
There was pygame.init(). There was screen = pygame.display.set_mode((size))with the variable size in position to be available in the global namespace.
有pygame.init()。有screen = pygame.display.set_mode((size))与位置在全局命名空间是可用的可变大小。
Turns out it was the main game loop.
原来这是主要的游戏循环。
# main game loop
while RUNNING == True:
for tneve in pygame.event.get():
if tneve.type == QUIT:
print(tneve)
RUNNING = False
loop()
render()
CLOCK.tick(FPS)
cleanup()
# End
What a pain!
多么痛苦!
P.S.
The problem is the one-stop-too-far indentation of everything below RUNNING = False.
PS问题是下面所有内容的一站式太远缩进RUNNING = False。
回答by sand24ro
You have to add:
你必须添加:
pygame.init()
Before you quit the display you should stop the while loop.
在退出显示之前,您应该停止 while 循环。
回答by Rajendro Sau
1 this problem happens when you using a beta version.
1 使用测试版时会出现此问题。
2 so my suggestion is please use the new, old version(If now lunch 3.8 python, you need to install python 3.7)
2 所以我的建议是请使用新的、旧的版本(如果现在午餐 3.8 python,你需要安装 python 3.7)
3 Now goto python terminal and install pygame (pip install pygame)
3 现在转到python终端并安装pygame(pip install pygame)
4 now the problem is solved...
4 现在问题解决了...
回答by Rajendro Sau
- If you doing
pygame.init()then solve the problem video system initialized. but you get the next error like:
- 如果这样做
pygame.init()则解决视频系统初始化的问题。但是您会收到下一个错误,例如:
(AttributeError:
tupleobject has no attribute 'fill') this.
(AttributeError:
tupleobject has no attribute 'fill') this.
- this problem is solving when you doing this
- 当你这样做时,这个问题正在解决
screen = pygame.display.set_mode((600, 400))
but not doing like
但不喜欢
screen = screen_width, screen_height = 600, 400
- Then the full problem is solved.
- 那么整个问题就解决了。

