我一直在尝试制作一个计时器,最近在mac上的python3中响起。在遇到这个bug之前一切都很好。这是我的代码:
import time
import pygame, sys
while True:
uin = input(">> ")
try:
when_to_stop = abs(int(uin))
except KeyboardInterrupt:
break
except:
print("Not a number!")
while when_to_stop >= 0:
m, s = divmod(when_to_stop, 60)
h, m = divmod(m, 60)
time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
print(time_left, "\r", end="")
time.sleep(1)
when_to_stop -= 1
print()
print('Times Up!')
display_width = 640
display_height = 480
screen=pygame.display.set_mode([display_width, display_height])
pygame.time.delay(1000)
pygame.init()
pygame.mixer.init()
soundwav=pygame.mixer.Sound("/Users/user/Desktop/Clock-ringing.wav")
soundwav.play()
while 1:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
当我在pygame窗口按下关闭按钮时出现此错误:
Traceback (most recent call last):
File "/Users/user/Desktop/countdown.py", line 37, in <module>
sys.exit()
SystemExit
有人可以告诉我原因吗?
答案 0 :(得分:0)
这是正常行为。 sys.exit()
总是抛出异常。请改用pygame.quit()
。