事件#

初始化事件类#

使用以下构造函数创建事件:

事件(回调,参数)

此构造函数使用两个参数:

范围

描述

回调

可选。 先前定义的函数,当事件广播时将作为线程被调用。

arg

**可选。**用于向事件回调函数传递参数的元组。

# 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()#

set(callback, arg) 方法为事件设置新的回调和参数。

参数

描述

打回来

事件触发时调用的新回调函数。

arg

**可选。**传递给回调函数的参数元组。

**返回:**无。

# Define a function event_occured()
def event_occured():
    brain.screen.print("event occured")

# Set the event to run event_occured() instead of print()
# when the event is broadcast.
event.set(event_occured)

broadcast()#

broadcast() 方法广播事件并导致所有已注册的回调函数运行。

**返回:**无。

# Broadcast event and run print().
event.broadcast()

broadcast_and_wait()#

broadcast_and_wait() 方法广播事件,导致所有已注册的回调函数运行,并等待所有回调函数完成。

**返回:**无。

# Broadcast event and wait for completion.
event.broadcast_and_wait()