Skip to content

Button

Create a clickable button that can be pressed by the user.

Pressing a Button calls a function, specified when the button is created.

Creating a Button

You can create a Button using the following functions:

Button()
Button(text, action, color)
Parameter Type Default Description
text str '' The text shown on the button.
action function None The function to call each time the button is pressed; it receives no parameters.
color Color Color.LIGHT_GRAY The button color.

For example,

button = Button("Play music", playMusic)

where playMusic is a function with zero parameters. This function will be called automatically when the user presses this button.

Once created, you can add it to a Display using the Display's add() function.

Functions

Once a Button button has been created, the following functions are available:

Function Description
button.getText() Return the object's text.
button.setText(text) Set the object's text.
button.setColor(color) Set the object's color.

Position

Function Description
button.getPosition() Return the object's position, the top-left corner of its bounding box.
button.getX() Return the object's horizontal position.
button.getY() Return the object's vertical position.
button.getCenter() Return the object's center point.
button.getCenterX() Return the object's horizontal center.
button.getCenterY() Return the object's vertical center.
button.setPosition(x, y) Move the object so the top-left corner of its bounding box sits at the given point.
button.setX(x) Set the object's horizontal position.
button.setY(y) Set the object's vertical position.
button.setCenter(x, y) Move the object so its center sits at the given point.
button.setCenterX(x) Set the object's horizontal center.
button.setCenterY(y) Set the object's vertical center.
button.move(x, y) Move the object to a new position.

Size

Function Description
button.getSize() Return the object's width and height.
button.getWidth() Return the object's width.
button.getHeight() Return the object's height.
button.setSize(width, height) Set the object's width and height.
button.setWidth(width) Set the object's width.
button.setHeight(height) Set the object's height.

Rotation

NOTE: Controls cannot be rotated. A control has these methods available, but they do nothing.

Function Description
button.setRotation(rotation) Do nothing, since controls cannot be turned.
button.getRotation() Return how far the object is turned.
button.rotate(angle) Turn the object by an additional angle.

Visibility

Function Description
button.getVisibility() Return how visible the object is.
button.setVisibility(visibility) Set how visible the object is.

Information

Function Description
button.getEndpoints() Return the object's four corners.
button.getBoundingBox() Return the smallest upright box that surrounds the object.
button.getGroup() Return the Group this object belongs to.
button.getDisplay() Return the Display this object is on.
button.setToolTipText() Set the hover text shown over the object.

Hit Testing

Function Description
button.contains(x, y) Report whether a point lies inside the object.
button.intersects(other) Report whether this object overlaps another.
button.encloses(other) Report whether this object completely contains another.

Events

Function Description
button.onMouseClick(action) Set up a function to call when the mouse is clicked on this object.
button.onMouseDown(action) Set up a function to call when the mouse button is pressed on this object.
button.onMouseUp(action) Set up a function to call when the mouse button is released over this object.
button.onMouseMove(action) Set up a function to call when the mouse moves over this object.
button.onMouseDrag(action) Set up a function to call when the mouse is dragged over this object.
button.onMouseEnter(action) Set up a function to call when the mouse moves onto this object.
button.onMouseExit(action) Set up a function to call when the mouse moves off this object.
button.onKeyType(action) Set up a function to call when a key is typed (pressed and released).
button.onKeyDown(action) Set up a function to call when a key is pressed down.
button.onKeyUp(action) Set up a function to call when a key is released.