事件#

初始化事件类#

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

Event(callback, arg)

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

范围

描述

callback

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

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)

This event object will be used in all subsequent examples throughout this API documentation when referring to Event class methods.

类方法#

set()#

The set(callback, arg) method sets a new callback and arguments for the event.

参数

描述

打回来

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

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

The broadcast() method broadcasts the event and causes all registered callback functions to run.

**返回:**无。

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

broadcast_and_wait()#

The broadcast_and_wait() method broadcasts the event, causes all registered callback functions to run, and waits until all callback functions have been completed.

**返回:**无。

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