Toggle
Create a toggle button that switches on and off with each click.
Toggles are drawn between a starting point (x1, y1) and an ending point (x2, y2).
Creating a Toggle
You can create a Toggle using the following functions:
Toggle(x1, y1, x2, y2)
Toggle(x1, y1, x2, y2, action, foregroundColor, backgroundColor, outlineColor, thickness, rotation, visibility)
| Parameter | Type | Default | Description |
|---|---|---|---|
x1 |
int or float |
required | The horizontal position of the top-left corner, in pixels. |
y1 |
int or float |
required | The vertical position of the top-left corner, in pixels. |
x2 |
int or float |
required | The horizontal position of the bottom-right corner, in pixels. |
y2 |
int or float |
required | The vertical position of the bottom-right corner, in pixels. |
action |
function |
None |
The function to call when the toggle changes; it receives the new value. |
foregroundColor |
Color |
Color.RED |
The color when on. |
backgroundColor |
Color |
Color.BLACK |
The color behind the toggle. |
outlineColor |
Color |
Color.CLEAR |
The outline color. |
thickness |
int |
3 |
The outline thickness, in pixels. |
rotation |
int or float |
0 |
How far to turn the toggle, in degrees, counter-clockwise. |
visibility |
int |
100 |
How visible the toggle is, from 0 (invisible) to 100 (fully visible). |
For example,
simpleToggle.py
# simpleToggle.py
# Creates a Toggle and prints its value when it changes.
from gui import *
d = Display()
# function to specify what happens when toggle is pressed
def printValue(value):
if value: # if value is True, push toggle is on
print("Yes") # replace this with whatever you want done when on
else: # else value is False (i.e., push toggle is off)
print("No") # replace this with whatever you want done when off (if any)
t = Toggle(25, 25, 50, 50, False, printValue, Color.WHITE, Color.BLACK, Color.RED, 1)
d.add(t)
Once created, you can add it to a Display using the Display's add() function.
Functions
Once a Toggle toggle has been created, the following functions are available:
| Function | Description |
|---|---|
toggle.getValue() |
Report whether the object is currently held down. |
toggle.setValue(newValue) |
Set whether the object is pressed. |
Position
| Function | Description |
|---|---|
toggle.getPosition() |
Return the object's position, the top-left corner of its bounding box. |
toggle.getX() |
Return the object's horizontal position. |
toggle.getY() |
Return the object's vertical position. |
toggle.getCenter() |
Return the object's center point. |
toggle.getCenterX() |
Return the object's horizontal center. |
toggle.getCenterY() |
Return the object's vertical center. |
toggle.setPosition(x, y) |
Move the object so the top-left corner of its bounding box sits at the given point. |
toggle.setX(x) |
Set the object's horizontal position. |
toggle.setY(y) |
Set the object's vertical position. |
toggle.setCenter(x, y) |
Move the object so its center sits at the given point. |
toggle.setCenterX(x) |
Set the object's horizontal center. |
toggle.setCenterY(y) |
Set the object's vertical center. |
toggle.move(x, y) |
Move the object to a new position. |
Size
| Function | Description |
|---|---|
toggle.getSize() |
Return the object's width and height. |
toggle.getWidth() |
Return the object's width. |
toggle.getHeight() |
Return the object's height. |
toggle.setSize(width, height) |
Set the object's width and height. |
toggle.setWidth(width) |
Set the object's width. |
toggle.setHeight(height) |
Set the object's height. |
Rotation
| Function | Description |
|---|---|
toggle.getRotation() |
Return how far the object is turned. |
toggle.setRotation(rotation) |
Turn the object to a given angle. |
toggle.rotate(angle) |
Turn the object by an additional angle. |
Visibility
| Function | Description |
|---|---|
toggle.getVisibility() |
Return how visible the object is. |
toggle.setVisibility(visibility) |
Set how visible the object is. |
Information
| Function | Description |
|---|---|
toggle.getEndpoints() |
Return the object's four corners. |
toggle.getBoundingBox() |
Return the smallest upright box that surrounds the object. |
toggle.getGroup() |
Return the Group this object belongs to. |
toggle.getDisplay() |
Return the Display this object is on. |
toggle.setToolTipText() |
Set the hover text shown over the object. |
Hit Testing
| Function | Description |
|---|---|
toggle.contains(x, y) |
Report whether a point lies inside the object. |
toggle.intersects(other) |
Report whether this object overlaps another. |
toggle.encloses(other) |
Report whether this object completely contains another. |
Events
| Function | Description |
|---|---|
toggle.onMouseClick(action) |
Set up a function to call when the mouse is clicked on this object. |
toggle.onMouseDown(action) |
Set up a function to call when the mouse toggle is pressed on this object. |
toggle.onMouseUp(action) |
Set up a function to call when the mouse toggle is released over this object. |
toggle.onMouseMove(action) |
Set up a function to call when the mouse moves over this object. |
toggle.onMouseDrag(action) |
Set up a function to call when the mouse is dragged over this object. |
toggle.onMouseEnter(action) |
Set up a function to call when the mouse moves onto this object. |
toggle.onMouseExit(action) |
Set up a function to call when the mouse moves off this object. |
toggle.onKeyType(action) |
Set up a function to call when a key is typed (pressed and released). |
toggle.onKeyDown(action) |
Set up a function to call when a key is pressed down. |
toggle.onKeyUp(action) |
Set up a function to call when a key is released. |