TextInput#

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

../../../_images/textinput-cocoa.png

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.

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#

class toga.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)#

Bases: Widget

Create a new single-line text input widget.

Parameters:
  • id – The ID for the widget.

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

  • value (str | None) – The initial content to display in the widget.

  • readonly (bool) – Can the value of the widget be modified by the user?

  • placeholder (str | None) – The content to display as a placeholder when there is no user content to display.

  • on_change (callable | None) – A handler that will be invoked when the the value of the widget changes.

  • on_confirm (callable | None) – A handler that will be invoked when the user accepts the value of the input (usually by pressing Return on the keyboard).

  • on_gain_focus (callable | None) – A handler that will be invoked when the widget gains input focus.

  • on_lose_focus (callable | None) – A handler that will be invoked when the widget loses input focus.

  • validators (list[callable] | None) – A list of validators to run on the value of the input.

property is_valid: bool#

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

property on_change: callable#

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

property on_confirm: callable#

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

property on_gain_focus: callable#

The handler to invoke when the widget gains input focus.

property on_lose_focus: callable#

The handler to invoke when the widget loses input focus.

property placeholder: str#

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().

property readonly: bool#

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.

property validators: list[callable]#

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

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

property value: str#

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.