summaryrefslogtreecommitdiff
path: root/docs/mainloop.txt
blob: 08344239196400a0812689a4741cd516eb5ca980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
.. _main-loop:

`MainLoop`
==========

The :class:`MainLoop` class ties together a :ref:`display-modules`, a set of
widgets and an :ref:`event-loops`. It handles passing input from the display
module to the widgets, rendering the widgets and passing the rendered canvas to
the display module to be drawn.

You may filter the user's input before it is passed to the widgets with your
own code by using :func:`input_filter`, or have special code to handle input
not handled by the widgets by using :func:`unhandled_input`.

You may set alarms to create timed events using :func:`set_alarm_at` or
:func:`set_alarm_in`. These methods automatically add a call to
:func:`draw_screen` after calling your callback. :func:`remove_alarm` may be
used to remove alarms.

When the main loop is running, any code that raises an :exc:`ExitMainLoop`
exception will cause the loop to exit cleanly. If any other exception reaches
the main loop code, it will shut down the screen to avoid leaving the terminal
in an unusual state then re-raise the exception for normal handling.

Using :class:`MainLoop` is highly recommended, but if it does not fit the needs
of your application you may choose to use your own code instead. There are no
dependencies on :class:`MainLoop` in other parts of Urwid.

`MainLoop reference <http://excess.org/urwid/reference.html#MainLoop>`_

Widgets Displayed
-----------------

The topmost widget displayed by :class:`MainLoop` must be passed as the first
parameter to the constructor. If you want to change the topmost widget while
running, you can assign a new widget to the `MainLoop` object's :attr:`widget`
attribute. This is useful for applications that have a number of different
modes or views.

The displayed widgets will be handling user input, so it is better to extend
the widgets that are displayed with your application-specific input handling so
that the application's behaviour changes when the widgets change. If all your
custom input handling is done from :func:`unhandled_input`, it will be
difficult to extend as your application gets more complicated. 

.. vim: set ft=rst: