python Pygame 错误:显示表面退出:为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1997710/
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: display surface quit: Why?
提问by Devo
Can anyone tell me why my app quits with:
谁能告诉我为什么我的应用程序退出:
pygame error: display Surface quit.
pygame 错误:显示 Surface 退出。
回答by Maciej Mi?sik
I had similar problem and discovered that Surface objects don't like to be deepcopied. When I used copy.deepcopy() on such object and then accessed the copy, I got that strange error message (without calling pygame.quit()). Maybe you experience similar behavior?
我遇到了类似的问题,发现 Surface 对象不喜欢被深度复制。当我在这样的对象上使用 copy.deepcopy() 然后访问副本时,我收到了那个奇怪的错误消息(没有调用 pygame.quit())。也许你遇到过类似的行为?
回答by Robert
I had a similar problem in a very simple piece of code:
我在一段非常简单的代码中遇到了类似的问题:
import sys, pygame
pygame.init()
size = width, height = 640, 480
speed = [2, 2]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("Golfball.png")
ballrect = ball.get_rect()
while 1:
event = pygame.event.poll()
if event.type == pygame.QUIT:
pygame.quit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
pygame.time.delay(5)
Error message was:
错误信息是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "bounce.py", line 22, in <module>
screen.fill(black)
pygame.error: display Surface quit
So I put
所以我把
import pdb
at the top after
在顶部之后
pygame.init()
and used
并使用
pdb.set_trace()
afterthe line with
在行之后
pygame.quit()
Now I ran the program, clicked to close the window and was actually a bit surprised to see that I fell into the debugger (after all, I expected the quit to completely take me out immediately). So I concluded that the quitdoesn't actually stop everything at that point. Looked like the program was continuing beyond the quit, was reaching
现在我运行程序,点击关闭窗口,看到我掉进调试器实际上有点惊讶(毕竟我预计退出会立即将我完全带出)。所以我得出结论,退出实际上并没有在那时停止一切。看起来程序在退出之后还在继续,正在到达
screen.fill(black)
and this was causing the problem. So I added
这导致了问题。所以我加了
break
after the
之后
pygame.quit()
and all works happily now.
现在一切都很愉快。
[ Added later: It occurs to me now that
[后来补充:我现在想到的是
pygame.quit()
is quitting the module, and not the program that is running, so you need the breakto get out of this simple program. ]
正在退出模块,而不是正在运行的程序,因此您需要休息一下才能退出这个简单的程序。]
Just for the record, this means the good version is
只是为了记录,这意味着好的版本是
import sys, pygame
pygame.init()
size = width, height = 640, 480
speed = [2, 2]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("Golfball.png")
ballrect = ball.get_rect()
while 1:
event = pygame.event.poll()
if event.type == pygame.QUIT:
pygame.quit()
break
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
pygame.time.delay(5)
回答by cCe.jbc
import pygame, sys
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # quit the screen
running = False
sys.exit()
call sys.exit() after pygame.quit() to stop the program so you can not change the surface after you have quit pygame and not get the error
在 pygame.quit() 之后调用 sys.exit() 以停止程序,因此在退出 pygame 后您无法更改表面并且不会收到错误
回答by Johan Bj?reholt
I had this problem too, but got it from another origin.
我也有这个问题,但它是从另一个来源得到的。
I had a class defined like this:
我有一个这样定义的类:
class PauseMenu(pygame.Surface)
i got the error when forgetting to initialize the pygame.Surface and trying to blit it, making pygame.screen crash and gave me this error.
我在忘记初始化 pygame.Surface 并尝试对其进行 blit 时遇到错误,导致 pygame.screen 崩溃并给了我这个错误。
so i just added this obviously
所以我只是明显地添加了这个
pygame.Surface.__init__(self, (width, height))
回答by Hamish Grubijan
From http://archives.seul.org/pygame/users/Nov-2006/msg00236.html:
来自http://archives.seul.org/pygame/users/Nov-2006/msg00236.html:
On Thu, 2006-11-30 at 21:27 -0300, Nicolas Bischof wrote:
2006 年 11 月 30 日星期四,21:27 -0300,Nicolas Bischof 写道:
pygame.error: display Surface quit what does that mean?, i can't fix it
pygame.error: display Surface quit 这意味着什么?,我无法修复
This means a call was made to pygame.display.quit() or pygame.quit(). If you try to do anything to the display surface after quit you will get this error.
这意味着调用了 pygame.display.quit() 或 pygame.quit()。如果您在退出后尝试对显示表面执行任何操作,您将收到此错误。
回答by hammythepig
I too had this problem, and similar to Maciej Mi?sik's answer mine had to do with copy.deepcopy()-ing an image.
我也有这个问题,类似于 Maciej Mi?sik 的回答,我的回答与 copy.deepcopy() 图像有关。
I had:
我有:
import copy,pygame,sys
from pygame.locals import *
EMPTY_IMG= pygame.image.load('C:super/fancy/file/path/transparent.png')
held_image=copy.deepcopy(EMPTY_IMG)
my_rect=held_image.get_rect()
my_rect.center = (50,50)
screen.blit(held_image,my_rect)
And I got the same error.
我得到了同样的错误。
I simply changed the copy.deepcopy(EMPTY_IMG) to just EMPTY_IMG.
我只是将 copy.deepcopy(EMPTY_IMG) 更改为 EMPTY_IMG。
import copy,pygame,sys
from pygame.locals import *
EMPTY_IMG= pygame.image.load('C:super/fancy/file/path/transparent.png')
held_image=EMPTY_IMG
my_rect=held_image.get_rect()
my_rect.center = (50,50)
screen.blit(held_image,my_rect)
Then it all worked fine.
然后一切正常。
回答by Thomas Edison
Replace if event.type == pygame.quit():
by if event.type == pygame.QUIT:
替换if event.type == pygame.quit():
为if event.type == pygame.QUIT:
回答by Pasquale Palombo
Make sure if you write pygame.QUIT:
and not pygame.quit():
I know it sounds weird, but I had the same problem.
确保你写pygame.QUIT:
而不是pygame.quit():
我知道这听起来很奇怪,但我有同样的问题。