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