English 中文(简体)
Pygame error: display surface quit: Why?
原标题:

Can anyone tell me why my app quits with:

pygame error: display Surface quit.

问题回答

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?

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

after the 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 quit doesn 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 break to 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)

Replace if event.type == pygame.quit(): by if event.type == pygame.QUIT:

Make sure if you write pygame.QUIT: and not pygame.quit(): I know it sounds weird, but I had the same problem.

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

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

On Thu, 2006-11-30 at 21:27 -0300, Nicolas Bischof wrote:

pygame.error: display Surface quit what does that mean?, i can t fix it

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.

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.

so i just added this obviously

pygame.Surface.__init__(self, (width, height))

I had the similar problem just right now and I have found a solution to it.

Because it seems like pygame.quit() will just quit the pygame module and not the entire program, use sys.exit() method on the following line.

After this:

pygame.quit()

Place this:

sys.exit()

Complete snippet:

pygame.quit()
sys.exit()

I too had this problem, and similar to Maciej Miąsik s answer mine had to do with copy.deepcopy()-ing an image.

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.

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.

Just update the display in the infinite loop.

Type this:

pygame.display.update()





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签