pygame 中的 sys.exit() 问题 [英] Issue with sys.exit() in pygame

查看:130
本文介绍了pygame 中的 sys.exit() 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用 Pygame,当我使用 sys.exit() 时,我遇到了一个问题.代码如下:

I am learning to use Pygame, and when I use sys.exit(), I run into a problem. Here is the code:

import pygame, sys,os
from pygame.locals import * 

pygame.init() 

window = pygame.display.set_mode((468, 60)) 
pygame.display.set_caption('Game') 
screen = pygame.display.get_surface() 

file_name = os.path.join("data","image.bmp")

surface = pygame.image.load(file_name)

screen.blit(surface, (0,0)) 
pygame.display.flip() 

def input(events): 
   for event in events: 
      if event.type == QUIT: 
         sys.exit(0) 
      else: 
         print event 

while True: 
   input(pygame.event.get()) 

这实际上只是 pygame 教程中的代码.无论我尝试对 sys.exit() 使用什么事件,当我实际尝试退出时都会出现问题.

It's really just the code from the pygame tutorial. The problem occurs when I actually try to exit, regardless of what event I try to use to sys.exit().

Traceback (most recent call last):
  File "C:/Python27/Lib/site-packages/pygame/examples/test.py", line 25, in <module>
    input(pygame.event.get())
  File "C:/Python27/Lib/site-packages/pygame/examples/test.py", line 20, in input
    sys.exit(0)
SystemExit: 0

... 然后程序不会退出.我在这里做错了什么?因为我确实注意到这段代码是针对过时的 Python 版本的.

... And then the program doesn't exit. What am I doing wrong here? Because I did notice that this code was for an antiquated version of Python.

推荐答案

sys.exit 只是抛出异常(SystemExit 异常).这有两个不寻常的影响:

sys.exit just throws an exception (a SystemExit exception). This has two unusual effects:

  1. 它只在多线程应用程序中退出当前线程
  2. 异常可能已被捕获.

这篇关于pygame 中的 sys.exit() 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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