关闭 Pygame 窗口 [英] Closing Pygame Window

查看:139
本文介绍了关闭 Pygame 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是花了相当多的时间找到 pygame 的 64 位安装以与 python 3.3 一起使用,(此处),现在正在尝试制作一个窗口.然而,虽然窗口打开得很好,但当它点击 x 按钮时并没有关闭.实际上,我必须关闭 IDLE 才能关闭窗口.我运行的是 64 位版本的 Win 7.这是我的代码:

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

当我追加

time.sleep(5)
pygame.quit()

它仍然没有关闭.我唯一的猜测是 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.

推荐答案

大多数 pygame 教程似乎都建议通过调用 pygame.quit()sys.exit() 退出代码>.我个人遇到过问题(虽然是在 unix 系统上),但仍然无法正确关闭窗口.解决方案是在 pygame.quit() 之前专门添加 pygame.display.quit().据我所知,这应该不是必要的,恐怕我不知道为什么这解决了问题,但确实解决了.

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.

这篇关于关闭 Pygame 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆