Skip to content
Toga
0.5.3.dev143+gabcc6cdb6

TextInput

A widget for the display and editing of a single line of text.

/reference/images/textinput-cocoa.png

/reference/images/textinput-gtk.png

/reference/images/textinput-winforms.png

/reference/images/textinput-android.png

/reference/images/textinput-iOS.png

/reference/images/textinput-web.png

Screenshot not available

Usage

import toga

text_input = toga.TextInput()
text_input.value = "Jane Developer"

The input can be provided a placeholder value - this is a value that will be displayed to the user as a prompt for appropriate content for the widget. This placeholder will only be displayed if the widget has no content; as soon as a value is provided (either by the user, or programmatically), the placeholder content will be hidden.

The input can also be provided a list of validators. A validator is a function that will be invoked whenever the content of the input changes. The function should return None if the current value of the input is valid; if the current value is invalid, it should return an error message. When on_change is invoked, the field will automatically be validated based on specified validators.

Notes

  • Although an error message is provided when validation fails, Toga does not guarantee that this error message will be displayed to the user.
  • Winforms does not support the use of partially or fully transparent colors for the TextInput background. If a color with an alpha value is provided (including TRANSPARENT), the alpha channel will be ignored. A TRANSPARENT background will be rendered as white.
  • On Winforms, if a TextInput is given an explicit height, the rendered widget will not expand to fill that space. The widget will have the fixed height determined by the font used on the widget. In general, you should avoid setting a height style property on TextInput widgets.

Reference

toga.TextInput

TextInput(
    id=None,
    style=None,
    value=None,
    readonly=False,
    placeholder=None,
    on_change=None,
    on_confirm=None,
    on_gain_focus=None,
    on_lose_focus=None,
    validators=None,
    **kwargs,
)

Bases: Widget

Create a new single-line text input widget.

PARAMETER DESCRIPTION
id

The ID for the widget.

TYPE: str | None DEFAULT: None

style

A style object. If no style is provided, a default style will be applied to the widget.

TYPE: StyleT | None DEFAULT: None

value

The initial content to display in the widget.

TYPE: str | None DEFAULT: None

readonly

Can the value of the widget be modified by the user?

TYPE: bool DEFAULT: False

placeholder

The content to display as a placeholder when there is no user content to display.

TYPE: str | None DEFAULT: None

on_change

A handler that will be invoked when the value of the widget changes.

TYPE: OnChangeHandler | None DEFAULT: None

on_confirm

A handler that will be invoked when the user accepts the value of the input (usually by pressing Return on the keyboard).

TYPE: OnConfirmHandler | None DEFAULT: None

on_gain_focus

A handler that will be invoked when the widget gains input focus.

TYPE: OnGainFocusHandler | None DEFAULT: None

on_lose_focus

A handler that will be invoked when the widget loses input focus.

TYPE: OnLoseFocusHandler | None DEFAULT: None

validators

A list of validators to run on the value of the input.

TYPE: Iterable[Callable[[str], bool]] | None DEFAULT: None

kwargs

Initial style properties.

DEFAULT: {}

is_valid property

is_valid

Does the value of the widget currently pass all validators without error?

on_change property writable

on_change

The handler to invoke when the value of the widget changes.

on_confirm property writable

on_confirm

The handler to invoke when the user accepts the value of the widget, usually by pressing return/enter on the keyboard.

on_gain_focus property writable

on_gain_focus

The handler to invoke when the widget gains input focus.

on_lose_focus property writable

on_lose_focus

The handler to invoke when the widget loses input focus.

placeholder property writable

placeholder

The placeholder text for the widget.

A value of None will be interpreted and returned as an empty string. Any other object will be converted to a string using str().

readonly property writable

readonly

Can the value of the widget be modified by the user?

This only controls manual changes by the user (i.e., typing at the keyboard). Programmatic changes are permitted while the widget has readonly enabled.

validators property writable

validators

The list of validators being used to check input on the widget.

Changing the list of validators will cause validation to be performed.

value property writable

value

The text to display in the widget.

A value of None will be interpreted and returned as an empty string. Any other object will be converted to a string using str().

Any newline (\n) characters in the string will be replaced with a space.

Validation will be performed as a result of changing widget value.

toga.widgets.textinput.OnChangeHandler

Bases: Protocol

__call__

__call__(widget, **kwargs)

A handler to invoke when the text input is changed.

PARAMETER DESCRIPTION
widget

The TextInput that was changed.

TYPE: TextInput

kwargs

Ensures compatibility with arguments added in future versions.

TYPE: Any DEFAULT: {}

toga.widgets.textinput.OnConfirmHandler

Bases: Protocol

__call__

__call__(widget, **kwargs)

A handler to invoke when the text input is confirmed.

PARAMETER DESCRIPTION
widget

The TextInput that was confirmed.

TYPE: TextInput

kwargs

Ensures compatibility with arguments added in future versions.

TYPE: Any DEFAULT: {}

toga.widgets.textinput.OnGainFocusHandler

Bases: Protocol

__call__

__call__(widget, **kwargs)

A handler to invoke when the text input gains focus.

PARAMETER DESCRIPTION
widget

The TextInput that gained focus.

TYPE: TextInput

kwargs

Ensures compatibility with arguments added in future versions.

TYPE: Any DEFAULT: {}

toga.widgets.textinput.OnLoseFocusHandler

Bases: Protocol

__call__

__call__(widget, **kwargs)

A handler to invoke when the text input loses focus.

PARAMETER DESCRIPTION
widget

The TextInput that lost focus.

TYPE: TextInput

kwargs

Ensures compatibility with arguments added in future versions.

TYPE: Any DEFAULT: {}