Skip to content

TextField

Create a single-line text field the user can type into.

If you create a TextField with a callback function, then that function will be called anytime the enter key is typed inside the box. (Presumably, the user will change the text and then press enter.)

Creating a TextField

You can create a TextField using the following functions:

TextField()
TextField(text, columns, action, color, font)
Parameter Type Default Description
text str '' The text to start with.
columns int 8 The width of the field, in characters.
action function None The function to call when the user presses Enter in the field; it receives one parameter, the field's contents as a string.
color Color Color.WHITE The field color.
font Font None The font, for example Font("Serif", Font.ITALIC, 16). If omitted, the default font is used.

For example,

textfield = TextField("type and hit <ENTER> ", 18, processEntry)

where processEntry is a function which expects one parameter, the updated text (a string).

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

Functions

Once a TextField textfield has been created, the following functions are available:

Function Description
textfield.getFont() Return the field's font.
textfield.setFont(font) Set the field's font.
textfield.setColor(color) Set the field's background color.

Checking State

If you create a TextField without a callback function, it is a passive GUI element. You can still check the text in the TextField using these functions:

Function Description
textfield.getText() Return the text in the field.
textfield.setText(text) Set the text in the field.

Position

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

Size

Function Description
textfield.getSize() Return the object's width and height.
textfield.getWidth() Return the object's width.
textfield.getHeight() Return the object's height.
textfield.setSize(width, height) Set the object's width and height.
textfield.setWidth(width) Set the object's width.
textfield.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
textfield.setRotation(rotation) Do nothing, since controls cannot be turned.
textfield.getRotation() Return how far the object is turned.
textfield.rotate(angle) Turn the object by an additional angle.

Visibility

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

Information

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

Hit Testing

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

Events

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