How do I solve "AttributeError: 'Event' object has no attribute 'key'"?
How do I solve "AttributeError: 'Event' object has no attribute 'key'"?
当我试图通过改变一个变量让我的角色行走并因此开始一个 animation.The 问题时,我的问题出现了但是我得到一个 AttributeError,其中“'Event' 对象没有属性 'key'”。我觉得很奇怪,因为我之前写过这个确切的代码,这个问题在以前的使用中从未出现过。我曾多次尝试重写代码,将其放在事件检查之外和内部,但没有任何效果。
while turnoff == False:
screen.fill(white)
# event checks
for event in pygame.event.get():
if event.type == pygame.QUIT:
turnoff = True
elif event.type == pygame.MOUSEBUTTONDOWN:
#supposed to identify which key but can't make it work
if event.key == pygame.K_DOWN:
action -= 1
frame = 0
if event.key == pygame.K_UP:
action -= 3
frame = 0
我得到的错误是
如果 event.key == pygame.K_DOWN:
AttributeError: 'Event' 对象没有属性 'key'
MOUSEBUTTONDOWN
事件没有 key
属性。但是,KEDOWN
事件具有 key
属性:
for event in pygame.event.get():
if event.type == pygame.QUIT:
turnoff = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
action -= 1
frame = 0
if event.key == pygame.K_UP:
action -= 3
frame = 0
错误是当事件是 pygame.MOUSEBUTTONDOWN
时您要求 event.key
。您只想在出现 pygame.KEYDOWN
时检查按键。您的代码应如下所示:
for event in pygame.event.get():
if event.type == pygame.QUIT:
turnoff = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
action -= 1
frame = 0
if event.key == pygame.K_UP:
action -= 3
frame = 0
MOUSEBUTTONDOWN
没有这样的 key
属性:
Read-only. The event type specific attributes of an event. The dict attribute is a synonym for backward compatibility.
For example, the attributes of a KEYDOWN event would be unicode, key, and mod
(source)
但是,根据 event.c
's source,可能会有 button
成员。也就是说,如果你想检查鼠标按钮。否则检查 event.type == pygame.KEYDOWN
.
当我试图通过改变一个变量让我的角色行走并因此开始一个 animation.The 问题时,我的问题出现了但是我得到一个 AttributeError,其中“'Event' 对象没有属性 'key'”。我觉得很奇怪,因为我之前写过这个确切的代码,这个问题在以前的使用中从未出现过。我曾多次尝试重写代码,将其放在事件检查之外和内部,但没有任何效果。
while turnoff == False:
screen.fill(white)
# event checks
for event in pygame.event.get():
if event.type == pygame.QUIT:
turnoff = True
elif event.type == pygame.MOUSEBUTTONDOWN:
#supposed to identify which key but can't make it work
if event.key == pygame.K_DOWN:
action -= 1
frame = 0
if event.key == pygame.K_UP:
action -= 3
frame = 0
我得到的错误是 如果 event.key == pygame.K_DOWN: AttributeError: 'Event' 对象没有属性 'key'
MOUSEBUTTONDOWN
事件没有 key
属性。但是,KEDOWN
事件具有 key
属性:
for event in pygame.event.get():
if event.type == pygame.QUIT:
turnoff = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
action -= 1
frame = 0
if event.key == pygame.K_UP:
action -= 3
frame = 0
错误是当事件是 pygame.MOUSEBUTTONDOWN
时您要求 event.key
。您只想在出现 pygame.KEYDOWN
时检查按键。您的代码应如下所示:
for event in pygame.event.get():
if event.type == pygame.QUIT:
turnoff = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
action -= 1
frame = 0
if event.key == pygame.K_UP:
action -= 3
frame = 0
MOUSEBUTTONDOWN
没有这样的 key
属性:
Read-only. The event type specific attributes of an event. The dict attribute is a synonym for backward compatibility.
For example, the attributes of a KEYDOWN event would be unicode, key, and mod (source)
但是,根据 event.c
's source,可能会有 button
成员。也就是说,如果你想检查鼠标按钮。否则检查 event.type == pygame.KEYDOWN
.