Pygame错误:显示表面退出:为什么?

9
有人能告诉我为什么我的应用程序会出现以下情况而退出吗?: pygame错误:显示表面退出。

4
请发布完整的回溯信息,至少包括引发异常的函数代码。 - Kylotan
3
没有提供最小工作示例的情况下,这个问题应该被关闭。但是,它有一些有价值的答案值得保留。 - Mogsdad
10个回答

11

我遇到了类似的问题,发现Surface对象不喜欢被深度复制。当我在这样的对象上使用copy.deepcopy()并访问复制时,我得到了那个奇怪的错误消息(没有调用pygame.quit())。也许你有类似的行为经历?


6

我在一段非常简单的代码中遇到了类似的问题:

    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)

错误信息如下:

    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

所以我放置了。
    import pdb

在顶部之后
    pygame.init()

并使用

    pdb.set_trace()

带有这行文字之后

    pygame.quit()

现在我运行了程序,点击关闭窗口,实际上有点惊讶地发现我进入了调试器(毕竟,我预计退出会立即完全将我带出)。因此,我得出结论:退出并没有真正停止那一点的所有内容。看起来程序在退出后仍在继续运行,正在到达。
    screen.fill(black)

这是导致问题的原因。所以我添加了

    break

在这之后

    pygame.quit()

现在一切都愉快地工作了。

[后来补充:我现在想到

    pygame.quit()

退出的是模块,而不是正在运行的程序,所以你需要使用break来退出这个简单的程序。

仅供参考,这意味着好的版本是:

    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)

6

if event.type == pygame.quit():替换为if event.type == pygame.QUIT:


3

确保你写的是pygame.QUIT:而不是pygame.quit(): 我知道这听起来很奇怪,但我遇到了同样的问题。


2
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()

在调用pygame.quit()之后使用sys.exit()来停止程序,以便在退出pygame后不再更改表面并避免出现错误。请注意保留HTML标签。

2

我刚刚遇到了类似的问题,并找到了解决方法。

由于看起来 pygame.quit() 只会退出 pygame 模块而不是整个程序,因此请在下一行使用 sys.exit() 方法。

完成后:

pygame.quit()

Place this:

sys.exit()

完整代码片段:

pygame.quit()
sys.exit()

1
我是一名有用的助手,可以为您翻译文本。

我也遇到了这个问题,但是我从另一个来源得到了它。

我定义了一个类,像这样:

class PauseMenu(pygame.Surface)

当我忘记初始化pygame.Surface并尝试blit它时,出现了错误,导致pygame.screen崩溃并给了我这个错误。所以我显然只需添加以下内容。
pygame.Surface.__init__(self, (width, height))

1

来自http://archives.seul.org/pygame/users/Nov-2006/msg00236.html :

在2006年11月30日星期四,Nicolas Bischof写道:

pygame.error: 显示界面已退出 这是什么意思?我无法解决

这意味着调用了pygame.display.quit()或pygame.quit()。 如果你在退出后尝试对显示屏面板做任何事情,就会得到这个错误。


0

我也遇到了这个问题,和Maciej Miąsik的答案类似,我的问题与copy.deepcopy()复制图像有关。

我有:

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)

我也遇到了同样的错误。

我只是将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)

然后一切都正常工作了。


-1

只需在无限循环中更新显示。

输入以下内容:

pygame.display.update()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接