English 中文(简体)
Pygame programs hanging on exit
原标题:

I m tinkering around with pygame right now, and it seems like all the little programs that I make with it hang when I try to close them.

Take the following code, for example:

from pygame.locals import *
pygame.init()
# YEEAAH!
tile_file = "blue_tile.bmp"
SCREEN_SIZE = (640, 480)
SCREEN_DEPTH = 32

if __name__ == "__main__":
    screen = pygame.display.set_mode(SCREEN_SIZE, 0, SCREEN_DEPTH)
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                break

    tile = pygame.image.load(tile_file).convert()
    colorkey = tile.get_at((0,0))
    tile.set_colorkey(colorkey, RLEACCEL)

    y = SCREEN_SIZE[1] / 2
    x = SCREEN_SIZE[0] / 2

    for _ in xrange(50):
        screen.blit(tile, (x,y))
        x -= 7
        y -= 14

I don t see anything wrong with the code, it works (ignore the fact that the tile isn t blit in the right spots), but there s no traceback and the only way to close it is to kill the python process in Task Manager. Can anyone spot a problem with my code?

问题回答

If you are running it from IDLE, then you are missing pygame.quit().

This is caused by the IDLE python interpreter, which seems to keep the references around somehow. Make sure, you invoke pygame.quit() on exiting your application or game.

See: In IDLE why does the Pygame window not close correctly?

And also: Pygame Documentation - pygame.quit()

Where do you exit the outer loop?

 while True: # outer loop
     for event in pygame.event.get(): # inner loop
         if event.type == QUIT:
            break # <- break inner loop

I had the same problem, but solved it by doing the following:

try:
   while True:
      for event in pygame.event.get():
         if event.type==QUIT or pygame.key.get_pressed()[K_ESCAPE]:
            pygame.quit()
            break
finally:
   pygame.quit()

if event.type==QUIT generates a syntax error. Should be == pygame.QUIT Also, the rest of the line is incorrect but I can t see how. There s a cleaner variant here:

    running = True
    while running:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
           running = False
    pygame.quit()

I had a similar problem on knowing why I can t close pygame windows.. and searched.. and came across this..

I think this explains everything.. and good idea too..

as seen in: http://bytes.com/topic/python/answers/802028-pygame-window-not-closing-tut-not-helping

I think the issue is that you re running it from within IDLE. It looks like pyGame s event loop and Tkinter s event loop interfere with each other. If you run the scripts from the command line, it works.





相关问题
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 ]="...

热门标签