Python pygame事件循环类型错误

时间:2013-04-27 16:35:13

标签: python events loops pygame typeerror

我正在做一个简单的pygame游戏。我的问题是,当我尝试检查用户是否单击退出按钮时,出现错误。这是代码:

for event in pygame.event.get():
   if event.type == pygame.QUIT():
       pygame.quit()
       sys.exit()

这是错误:

Traceback (most recent call last):
File "C:\Users\Rafi\Python Programs\Game.py", line 20, in <module>
if event.type == pygame.QUIT():
TypeError: 'int' object is not callable

此外,这可能不是,但我在Windows 8上。

2 个答案:

答案 0 :(得分:2)

>>> pygame.QUIT
12

所以,

>>> pygame.QUIT() >> 12()
TypeError: 'int' object is not callable

在文字中,pygame.QUIT = 12这样做pygame.QUIT()相当于做12(),这是一个电话,这不是你想要的。

只需将您的行更改为:

if event.type == pygame.QUIT:

答案 1 :(得分:0)

pygame.QUIT是一个常量(如果你想知道,则为12)。它之后不需要任何()。这就是你的全部问题。