【问题标题】:Pygame if event.type == pygame.KEYDOWN() TypeError: 'int' object is not callablePygame if event.type == pygame.KEYDOWN() TypeError: 'int' object is not callable
【发布时间】:2015-01-05 11:31:40
【问题描述】:

该代码旨在通过点击escape快速退出pygame。

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN():
            if event.key == pygame.K_ESCAPE:
                running = False
                pygame.quit()
                sys.exit()

此代码错误:

Message File Name   Line    Position    
Traceback               
    <module>    G:\Code\JonSocket\keyboard.py   19      
TypeError: 'int' object is not callable             

非常相似的代码在什么时候起作用,

event.type == pygame.QUIT

pygame.QUIT + pygame.KEYDOWN 有区别吗?

谢谢。

【问题讨论】:

  • 最小复制案例:x = 1; x()。确保听取错误消息 - 这太本地化并且非常重复。
  • 感谢您的回答。我解决了,就像 cmets 说的那样,将其更改为 if event.type == pygame.KEYDOWN:
  • pygame.quit 是一个函数,但 pygame.QUIT 是一个 int。您可以使用print(type(pygame.QUIT)) 验证这一点
  • “非常相似的代码有效” - 不,它没有。 pygame.QUIT() 抛出相同的错误。 pygame.QUIT 是一个整数(准确地说是 12)。 pygame.quit() 是一个函数。 Python 区分大小写。

标签: python pygame


【解决方案1】:

你的问题出在一行

if event.type == pygame.KEYDOWN():

pygame.KEYDOWN 不是一个函数,而只是一个整数常量。

改成

if event.type == pygame.KEYDOWN:

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 2017-04-06
    • 2013-03-30
    • 2022-12-26
    • 2018-07-23
    • 2018-03-31
    • 2018-04-14
    • 2019-11-11
    相关资源
    最近更新 更多