python - 按钮不在事件内?
问题描述
我正在为我的 xbox one 控件使用 pygame,应该有一些名为按钮的东西,但它会抛出一个错误说AttributeError: 'Event' object has no attribute 'button'
这是整个代码
import pygame
pygame.init()
joysticks = []
clock = pygame.time.Clock()
keepPlaying = True
# for al the connected joysticks
for i in range(0, pygame.joystick.get_count()):
# create an Joystick object in our list
joysticks.append(pygame.joystick.Joystick(i))
# initialize them all (-1 means loop forever)
joysticks[-1].init()
# print a statement telling what the name of the controller is
print ("Detected joystick "),joysticks[-1].get_name(),"'"
while keepPlaying:
for event in pygame.event.get():
print(event)
if event.button == 0:
print ("A Has Been Pressed")
当我按下它时,它会打印<Event(1539-JoyButtonDown {'joy': 0, 'instance_id': 0, 'button': 0})>
出一个按钮,但它会抛出错误
解决方案
每种事件类型都会生成一个pygame.event.Event
具有不同属性的对象。button
没有为所有事件对象定义该属性。您可以button
从鼠标或操纵杆事件(如JOYBUTTONUP
or )中获取属性JOYBUTTONDOWN
。然而,所有事件对象都有一个type
属性。type
在属性之前检查事件属性button
(请参阅pygame.event
):
while keepPlaying:
for event in pygame.event.get():
print(event)
if event.type == JOYBUTTONDOWN:
if event.button == 0:
print ("A Has Been Pressed")
推荐阅读
- ssis - 如果文件下载失败,如何在一段时间后重新运行 SSIS 包?
- csv - 如果从 UTF-16 LE(使用 -u)转换为 UTF-8,使用 unicode 导出的 csvde 文件是否会丢失字符?
- amazon-web-services - 保存数据的最佳方法 Lambda 函数
- android - 如何修复 ActivityThread 致命异常:java.lang.RuntimeException?
- python - 未定义的变量,在 if 之后
- java - 如何将 Android 软键盘移动到屏幕顶部?
- pyspark - 在 pyspark 中使用窗口函数时,结果 parquet 文件大小变大
- php - 如何将 XML 文件中的值添加到 php 中的 csv 文件?
- java - 肥皂请求 XML。来自 WSDL
- graphql - Hasura 选择子项包含当前用户的项目