[英]AttributeError: 'Event' object has no attribute 'button'
Here's my Code and I am just trying run my xbox controller for later on if statements to control dc motors:这是我的代码,我只是尝试运行我的 xbox controller 以便稍后使用 if 语句来控制直流电机:
pygame.init()
joystick = []
clock = pygame.time.Clock()
for i in range(0, pygame.joystick.get_count()):
joystick.append(pygame.joystick.Joystick(i))
joystick[-1].init()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.button == 0:
print ("A Has Been Pressed")
elif event.button == 1:
print ("B Has Been Pressed")
elif event.button == 2:
print ("X Has Been Pressed")
elif event.button == 3:
print ("Y Has Been Pressed")
I get the error message below when I run my code:运行代码时,我收到以下错误消息:
pygame 1.9.4.post1
Hello from the pygame community. https://www.pygame.org/contribute.html
A Has Been Pressed
Traceback (most recent call last):
File "/home/pi/Documents/Code/Practice.py", line 13, in <module>
if event.button == 0:
AttributeError: 'Event' object has no attribute 'button'
Each event type generates a pygame.event.Event
object with different attributes.每种事件类型都会生成一个具有不同属性的
pygame.event.Event
object。 The button
attribute is not defined for all event objects.没有为所有事件对象定义
button
属性。 You can just get the button
attribute from a mouse or joystick event like MOUSEMOTION
or MOUSEBUTTONDOWN
.您可以从鼠标或操纵杆事件(如
MOUSEMOTION
或MOUSEBUTTONDOWN
)中获取button
属性。 However all event objects have a type
attribute.然而,所有事件对象都有一个
type
属性。 Check the event type
attribute before the button
attribute (see pygame.event
):在
button
属性之前检查事件type
属性(参见pygame.event
):
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 0:
print ("A Has Been Pressed")
elif event.button == 1:
print ("B Has Been Pressed")
elif event.button == 2:
print ("X Has Been Pressed")
elif event.button == 3:
print ("Y Has Been Pressed")
The MOUSEBUTTONDOWN
event occurs once when you click the mouse button and the MOUSEBUTTONUP
event occurs once when the mouse button is released. MOUSEBUTTONDOWN
事件在您单击鼠标按钮时发生一次,而MOUSEBUTTONUP
事件在释放鼠标按钮时发生一次。 The pygame.event.Event()
object has two attributes that provide information about the mouse event. pygame.event.Event()
object 有两个属性提供有关鼠标事件的信息。 pos
is a tuple that stores the position that was clicked. pos
是一个存储被点击的 position 的元组。 button
stores the button that was clicked. button
存储被点击的按钮。 Each mouse button is associated a value.每个鼠标按钮都关联一个值。 For instance the value of the attributes is 1, 2, 3, 4, 5 for the left mouse button, middle mouse button, right mouse button, mouse wheel up respectively mouse wheel down.
例如,鼠标左键、鼠标中键、鼠标右键、鼠标滚轮向上和鼠标滚轮向下的属性值为 1、2、3、4、5。 When multiple keys are pressed, multiple mouse button events occur.
当按下多个键时,会发生多个鼠标按钮事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.