【发布时间】:2010-05-12 14:51:37
【问题描述】:
我编写了一个小型 Python 应用程序,在其中使用 PyGame 显示一些简单的图形。
我的应用程序的基础中有一个简单的 PyGame 循环,如下所示:
stopEvent = Event()
# Just imagine that this eventually sets the stopEvent
# as soon as the program is finished with its task.
disp = SortDisplay(algorithm, stopEvent)
def update():
""" Update loop; updates the screen every few seconds. """
while True:
stopEvent.wait(options.delay)
disp.update()
if stopEvent.isSet():
break
disp.step()
t = Thread(target=update)
t.start()
while not stopEvent.isSet():
for event in pygame.event.get():
if event.type == pygame.QUIT:
stopEvent.set()
对于正常的程序终止来说,它工作得很好而且很花哨;如果 PyGame 窗口关闭,则应用程序关闭;如果应用程序完成其任务,则应用程序关闭。
我遇到的问题是,如果我在 Python 控制台中 Ctrl-C,应用程序会抛出 KeyboardInterrupt,但会继续运行。
因此问题是:我在更新循环中做错了什么,我该如何纠正它以使KeyboardInterrupt 导致应用程序终止?
【问题讨论】: