Behaviors can be defined as a condition-action pair. If the condition is satisfied then the action is triggered. This is a simple but very powerful mechanism that can be used to create sophisticated programs. Some condition definitions may require additional parameters. Currently the conditions and actions that can be created from the behavior form (see Figure 1) are limited in scope. Conditions are limited to
and available actions can be grouped as
In the near future more condition and action types will be added to the behavior form. In addition, compound conditions (conditions grouped with boolean operators) will be added.
For Python programmers, there is no limit for creating conditions or actions. Examples section include many frequently used techniques for simple scenarios and more complex behavior can be modeled by combining them.
Automatically generated code (generated by the behavior dialog):
# Create a circle when left mouse button is released.
rectangle1.bind(LEFTUP, system.evtHandler, CREATECIRCLE,
**{'x': 20, 'y': 30, 'radius': 3})
Same behavior can be modeled by using named arguments in both function definition and binding definition,
def createCircle(event, source, x, y, radius):
circle(x, y, r)
circle1.bind(LEFTUP, createCircle, x=20, y=30, radius=3})
or named arguments in function definition and positional arguments in binding definition,
def createCircle(event, source, x, y, radius):
circle(x, y, r)
circle1.bind(LEFTUP, createCircle, 20, 30, 3})
or named argument lists in function definition and variable-length positional arguments in binding definition,
def createCircle(event, source, x, y, radius):
circle(x, y, radius)
circle1.bind(LEFTUP, createCircle, *(20, 30, 3))
or variable-length positional arguments in both function and binding definition,
def createCircle(event, source, *args):
circle(*args)
circle1.bind(LEFTUP, createCircle, *(20, 30, 3))
or named arguments in function definition and keyword arguments in binding definition,
def createCircle(event, source, x, y, radius):
circle(x, y, radius)
circle1.bind(LEFTUP, createCircle, **{'x': 20, 'y': 30, 'radius': 3})
or keyword arguments in both function and binding definition,
def createCircle(event, source, **kwords):
circle(**kwords)
circle1.bind(LEFTUP, createCircle, **{'x': 20, 'y': 30, 'radius': 3})
More combinations can be listed but the samples shown here cover the most commonly used cases.
In some cases you may want to bind the same function to several different elements to have short and concise code. Examples below demonstrate various use cases of event bindings to multiple elements.
As an example we are going to use a left mouse button click as a condition and orientation angle adjustment as an action.
Automatically generated code (generated by the behavior dialog):
# Adjust circle1's angle when left mouse button is pressed down.
circle1.bind(LEFTDOWN, system.evtHandler, ADJUSTANGLE, **{'d_angle': .52})
Same behavior can be modeled as
def adjustAngle(event, source):
circle1.angle += .52
circle1.bind(LEFTDOWN, adjustAngle)
If you would like to apply the same action to multiple elements
def adjustAngle(event, source):
source.angle += .52
circle1.bind(LEFTDOWN, adjustAngle)
rectangle1.bind(LEFTDOWN, adjustAngle)
To apply different rotations to different elements
def adjustAngle(event, source, dAngle):
source.angle += dAngle
circle1.bind(LEFTDOWN, adjustAngle, dAngle=.3)
rectangle1.bind(LEFTDOWN, adjustAngle, dAngle=.5)
In the code snippets below we will be showing mostly named arguments for single elements but please keep in mind that the forms shown above can be used instead.