= Attaching To Events =
== Statically in RML ==
The easiest way to attach to events with python is to write your code directly into the RML files, using the on* attributes. When the event is fired three global variables are set up, ''document'', ''event'' and ''self''.
||self||The element thats currently being processed||
||[wiki:documentation/PythonManual/Documents document]||The document the element thats currently being processed belongs to||
||[wiki:documentation/PythonManual/Events event]||The event thats currently being processed||
Example:
{{{
}}}
To aid in the coding of inline Python code, libRocket allows multiple lines of Python code can be put on one line, separated by a semicolon. The parser will then reformat this code before passing it to the Python interpreter.
Example:
{{{
}}}
== Dynamically from Python Code ==
The Python version of AddEventListener is modelled directly on Javascript. This allows you to bind any callable Python object (free function or method) or string to an event.
Method 1:
{{{
element = document.GetElementById('button')
element.AddEventListener('click', "print('Line 1');print('Line 2')", True)
}}}
Method 2:
{{{
def OnClick():
for i in range(10):
print('Line ' + str(i))
element = document.GetElementById('button')
element.AddEventListener('onclick', OnClick, True)
}}}