事件#
初始化事件类#
Event
构造函数创建一个 Event 对象。
此构造函数使用两个参数:
范围 |
描述 |
---|---|
|
可选。 先前定义的函数,当事件广播时将作为线程被调用。 |
|
**可选。**用于向事件回调函数传递参数的元组。 |
# Define the function broadcasted_event().
def broadcasted_event():
brain.screen.print("Event was broadcast")
# Construct an Event "event" with the
# Event class.
event = Event(broadcasted_event)
当引用 Event 类方法时,此“event”对象将在本 API 文档的所有后续示例中使用。
类方法#
放()#
set(callback, arg)
方法为事件设置新的回调和参数。
参数 |
描述 |
---|---|
打回来 |
事件触发时调用的新回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无。
# Define a function event_occurred()
def event_occurred():
brain.screen.set_cursor(1, 1)
brain.screen.print("event occurred")
# Set the event to run event_occurred() instead of print()
# when the event is broadcast.
event = Event(event_occurred)
event.set(event_occurred)
# Broadcast event and run print().
event.broadcast()
播送()#
broadcast()
方法广播事件并导致所有已注册的回调函数运行。
**返回:**无。
def button_pressed():
brain.play_sound(SoundType.ALARM)
wait(0.5,SECONDS)
# Broadcast the event when the button is held down
# and turn when it is not.
event = Event(button_pressed)
event.set(button_pressed)
while True:
if brain.buttonCheck.pressing():
event.broadcast()
else:
drivetrain.turn(RIGHT)
广播并等待()#
broadcast_and_wait()
方法广播事件,导致所有已注册的回调函数运行,并等待所有回调函数完成。
**返回:**无。
def forward_and_turn():
drivetrain.drive_for(FORWARD, 100, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
# Wait for a button to be pressed,then wait for
# the robot to be done moving and print a message.
event = Event(forward_and_turn)
event.set(forward_and_turn)
while not brain.buttonCheck.pressing():
wait(0.1,SECONDS)
event.broadcast_and_wait()
brain.screen.print("Done!")