Python 关闭 Pygame 窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19882415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Closing Pygame Window
提问by KnightOfNi
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:
我只是花了相当多的时间找到 pygame 的 64 位安装以与 python 3.3 一起使用,(这里)现在我正在尝试制作一个窗口。然而,虽然窗口打开得很好,但当它点击 x 按钮时并没有关闭。实际上,我必须关闭 IDLE 才能关闭窗口。我正在运行 64 位版本的 Win 7。这是我的代码:
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.
它仍然没有关闭。我唯一的猜测是 pygame.quit 可能会进入其中一个循环,但即使解决了这个问题,我也非常希望能够在我想要的时候关闭窗口。
回答by noobmaster69
Not sure but try this Because you code runs fine on my system after I add pygame.quit()
at the end
不知道,但尝试是因为你的代码运行我的系统上罚款后,我加pygame.quit()
末
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.
这可能是因为 Idle 是在 Tkinter 上制作的,因此 Tkinter 和 Pygame 主循环没有相互理解。
不过,您的代码将在命令提示符下运行良好。
回答by Erikun
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 教程似乎建议通过调用pygame.quit()
然后退出sys.exit()
。我个人遇到过问题(虽然是在 unix 系统上),但仍然无法正确关闭窗口。解决方案是pygame.display.quit()
在pygame.quit()
. 据我所知,这应该没有必要,恐怕我不知道为什么这解决了问题,但确实解决了。
回答by OphirBack
try using the following command:
尝试使用以下命令:
sys.exit(0)
sys.exit(0)
notice: You will need to import the sys library in order to use it.
注意:您需要导入 sys 库才能使用它。
回答by jerryalfs
if you want to make pygame close when window button x is pressed, put the code like this:
如果要在按下窗口按钮 x 时关闭 pygame,请将代码如下:
from sys import exit
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
We put exit() after pygame.quit(), because pygame.quit() makes the system exit and exit() closes that window.
我们将 exit() 放在 pygame.quit() 之后,因为 pygame.quit() 使系统退出,而 exit() 关闭该窗口。
回答by NinjasAtWork
The IDE interferes with how pygame runs the code. Try to run it from the commandline or the terminal. The problem should disappear.
IDE 会干扰 pygame 运行代码的方式。尝试从命令行或终端运行它。问题应该会消失。
回答by Bruno Cavalleri
To answer the original question: You must call pygame.quit()
after breaking the main loop. One elegant solution goes as follows:
回答原始问题:您必须pygame.quit()
在中断主循环后调用。一种优雅的解决方案如下:
def run():
pygame.init()
while True:
# ...
for event in pygame.event.get():
# Handle other events
if event.type == pygame.QUIT:
return pygame.quit()
回答by blissweb
This was the final code that worked for me on OSX whilst keeping the kernel alive on Jupyter. EDIT - it does still crash the kernel sometimes :-(
这是在 OSX 上为我工作同时在 Jupyter 上保持内核活动的最终代码。编辑 - 它有时仍然会使内核崩溃:-(
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.quit()
pygame.quit()
exit()
Also needed to downgrade ipython to get rid of some magic alias warning messages using:
还需要降级 ipython 以使用以下方法摆脱一些神奇的别名警告消息:
conda install ipython=7.2.0
apparently that issue is due to be fixed in ipython 7.6.0
显然,该问题将在 ipython 7.6.0 中修复
回答by Jose Pablo Jimenez
Suffered the same issues on Python 3.7.4 while running it from in IDE (Spyder 3.3.6). In my case the pygame.quit() would not completely close the program. Nonetheless, adding quit() or exit() did the trick for me!
在 IDE (Spyder 3.3.6) 中运行 Python 3.7.4 时遇到了同样的问题。就我而言, pygame.quit() 不会完全关闭程序。尽管如此,添加 quit() 或 exit() 对我有用!