Closing Pygame Window

2019-03-26 08:28发布

I just spent a fair amount of time finding a 64-bit installation of pygame to use with python 3.3, (here) and now am trying to make a window. However, although the window opens up fine it does not close when it hit the x button. In fact, I have to close IDLE to close the window. I am running a 64 bit version of Win 7. Here is my code:

import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

When I append

time.sleep(5)
pygame.quit()

It still doesn't close. My only guess would be that pygame.quit might go inside one of the loops, but even if that were resolved I would greatly prefer being able to close the window when I want to.

标签: python pygame
6条回答
Anthone
2楼-- · 2019-03-26 08:37

Not sure but try this Because you code runs fine on my system after I add pygame.quit() at the end

import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
try:
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    pygame.quit()
except SystemExit:
    pygame.quit()

Its perhaps because as Idle is made on Tkinter and thus Tkinter and Pygame main loop do not have a mutual understanding.
Your code will run very well on command prompt though.

查看更多
乱世女痞
3楼-- · 2019-03-26 08:37

try using the following command:

sys.exit(0)

notice: You will need to import the sys library in order to use it.

查看更多
\"骚年 ilove
4楼-- · 2019-03-26 08:41

To answer the original question: You must call pygame.quit() after breaking the main loop. One elegant solution goes as follows:

def run():
    pygame.init()
    while True:
        # ...
        for event in pygame.event.get():
            # Handle other events
            if event.type == pygame.QUIT:
                return pygame.quit()
查看更多
倾城 Initia
5楼-- · 2019-03-26 08:51

Most pygame tutorials seem to suggest exiting by calling pygame.quit() and then sys.exit(). I have personally run into problems (was on a unix system though) where this still did not close the window properly. The solution was to add pygame.display.quit() specifically before pygame.quit(). That should not be necessary as far as I can tell, and I'm afraid I don't know why that solved the issue but it did.

查看更多
来,给爷笑一个
6楼-- · 2019-03-26 08:53

if wanna make pygame close when window button x press put the code like this

from sys import exit
while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

why put exit() after pygame.quit() cause pygame.quit() make system exit and exit() for close that window

查看更多
聊天终结者
7楼-- · 2019-03-26 09:02

The IDE interferes with how pygame runs the code. Try to run it from the commandline or the terminal. The problem should disappear.

查看更多
登录 后发表回答