如何在不冻结的情况下在角落中使用[x]关闭窗口?

2024-04-26 01:20:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想单击窗口一角的[x],让它关闭整个应用程序。相反,它正在冻结,没有反应。谢谢你的帮助。你知道吗

import sys
#import and init pygame
import pygame
pygame.init() 

#create the screen
window = pygame.display.set_mode((640, 480)) 

#draw a line - see http://www.pygame.org/docs/ref/draw.html for more 
pygame.draw.line(window, (255, 255, 255), (0, 0), (30, 50))

#draw it to the screen
pygame.display.flip() 

#input handling (somewhat boilerplate code):
while True: 
for event in pygame.event.get(): 
   if event.type == pygame.QUIT: 
      sys.exit(0) 
  else: 
      print event 

我已经习惯了java,所以我想知道是否有一个dispose和close类型的动作。你知道吗


Tags: andtheimportevent应用程序forinitcreate
1条回答
网友
1楼 · 发布于 2024-04-26 01:20:32

尝试在sys.exit(0)之前添加pygame.quit()以关闭pygame窗口,如下所示:

import sys

import pygame
pygame.init() 

window = pygame.display.set_mode((640, 480)) 
pygame.draw.line(window, (255, 255, 255), (0, 0), (30, 50))
pygame.display.flip() 

while True: 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            pygame.quit()
            sys.exit(0) 
        else: 
            print event 

相关问题 更多 >