Changes from Version 1 of documentation/PythonManual/AttachingToEvents

Show
Ignore:
Author:
lloydw (IP: 203.96.63.172)
Timestamp:
02/27/08 11:25:18 (10 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • documentation/PythonManual/AttachingToEvents

    v0 v1  
     1= Attaching To Events = 
     2 
     3 
     4== Statically in RML == 
     5The 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''. 
     6 
     7||self||The element thats currently being processed|| 
     8||[wiki:documentation/PythonManual/Documents document]||The document the element thats currently being processed belongs to|| 
     9||[wiki:documentation/PythonManual/Events event]||The event thats currently being processed|| 
     10 
     11Example: 
     12{{{ 
     13<button onclick="print('Clicked!')"/> 
     14}}} 
     15 
     16To 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. 
     17 
     18Example: 
     19{{{ 
     20<button onclick="print('Line 1');print('Line 2')"/> 
     21}}} 
     22 
     23 
     24== Dynamically from Python Code == 
     25 
     26The 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. 
     27 
     28Method 1: 
     29{{{ 
     30element = document.GetElementById('button') 
     31element.AddEventListener('onclick', "print('Line 1');print('Line 2')", True) 
     32}}} 
     33 
     34Method 2: 
     35{{{ 
     36def OnClick(): 
     37  for i in range(10): 
     38    print('Line ' + str(i)) 
     39 
     40element = document.GetElementById('button') 
     41element.AddEventListener('onclick', OnClick, True) 
     42}}} 
     43