Skip to content

Message pump

A message pump is a class that processes messages.

It is a base class for the App, Screen, and Widget classes.

message_pump

A message pump is a class that processes messages.

It is a base class for the App, Screen, and Widgets.

MessagePump

app: App property

Get the current app.

Returns:

Type Description
App

The current app.

Raises:

Type Description
NoActiveAppError

if no active app could be found for the current asyncio context

is_attached: bool property

Is the node is attached to the app via the DOM.

is_running: bool property

log: Logger property

Get a logger for this object.

Returns:

Type Description
Logger

A logger.

call_after_refresh(callback, *args, **kwargs)

Schedule a callback to run after all messages are processed and the screen has been refreshed. Positional and keyword arguments are passed to the callable.

Parameters:

Name Type Description Default
callback Callable

A callable.

required

call_later(callback, *args, **kwargs)

Schedule a callback to run after all messages are processed in this object. Positional and keywords arguments are passed to the callable.

Parameters:

Name Type Description Default
callback Callable

Callable to call next.

required
*args

Positional arguments to pass to the callable.

()
**kwargs

Keyword arguments to pass to the callable.

{}

call_next(callback, *args, **kwargs)

Schedule a callback to run immediately after processing the current message.

Parameters:

Name Type Description Default
callback Callable

Callable to run after current event.

required
*args

Positional arguments to pass to the callable.

()
**kwargs

Keyword arguments to pass to the callable.

{}

check_idle()

Prompt the message pump to call idle if the queue is empty.

check_message_enabled(message)

Check if a given message is enabled (allowed to be sent).

Parameters:

Name Type Description Default
message Message

A message object.

required

Returns:

Type Description
bool

True if the message will be sent, or False if it is disabled.

disable_messages(*messages)

Disable message types from being processed.

dispatch_key(event) async

Dispatch a key event to method.

This method will call the method named 'key_' if it exists. Some keys have aliases. The first alias found will be invoked if it exists. If multiple handlers exist that match the key, an exception is raised.

Parameters:

Name Type Description Default
event events.Key

A key event.

required

Returns:

Type Description
bool

True if key was handled, otherwise False.

Raises:

Type Description
DuplicateKeyHandlers

When there's more than 1 handler that could handle this key.

enable_messages(*messages)

Enable processing of messages types.

on_event(event) async

Called to process an event.

Parameters:

Name Type Description Default
event events.Event

An Event object.

required

post_message(message)

Posts a message on the queue.

Parameters:

Name Type Description Default
message Message

A message (or Event).

required

Returns:

Type Description
bool

True if the messages was processed, False if it wasn't.

prevent(*message_types)

A context manager to temporarily prevent the given message types from being posted.

Example
input = self.query_one(Input)
with self.prevent(Input.Changed):
    input.value = "foo"

set_interval(interval, callback=None, *, name=None, repeat=0, pause=False)

Call a function at periodic intervals.

Parameters:

Name Type Description Default
interval float

Time between calls.

required
callback TimerCallback | None

Function to call. Defaults to None.

None
name str | None

Name of the timer object. Defaults to None.

None
repeat int

Number of times to repeat the call or 0 for continuous. Defaults to 0.

0
pause bool

Start the timer paused. Defaults to False.

False

Returns:

Type Description
Timer

A timer object.

set_timer(delay, callback=None, *, name=None, pause=False)

Make a function call after a delay.

Parameters:

Name Type Description Default
delay float

Time to wait before invoking callback.

required
callback TimerCallback | None

Callback to call after time has expired. Defaults to None.

None
name str | None

Name of the timer (for debug). Defaults to None.

None
pause bool

Start timer paused. Defaults to False.

False

Returns:

Type Description
Timer

A timer object.

MessagePumpMeta

Bases: type

Metaclass for message pump. This exists to populate a Message inner class of a Widget with the parent classes' name.