TextInput¶
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. ATRANSPARENTbackground 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
heightstyle property on TextInput widgets.
Reference¶
Bases: Widget
Source code in core/src/toga/widgets/textinput.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
is_valid
property
¶
Does the value of the widget currently pass all validators without error?
on_change
property
writable
¶
The handler to invoke when the value of the widget changes.
on_confirm
property
writable
¶
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
¶
The handler to invoke when the widget gains input focus.
on_lose_focus
property
writable
¶
The handler to invoke when the widget loses input focus.
placeholder
property
writable
¶
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
¶
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
¶
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
¶
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.
__init__(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)
¶
Create a new single-line text input widget.
:param id: The ID for the widget. :param style: A style object. If no style is provided, a default style will be applied to the widget. :param value: The initial content to display in the widget. :param readonly: Can the value of the widget be modified by the user? :param placeholder: The content to display as a placeholder when there is no user content to display. :param on_change: A handler that will be invoked when the value of the widget changes. :param on_confirm: A handler that will be invoked when the user accepts the value of the input (usually by pressing Return on the keyboard). :param on_gain_focus: A handler that will be invoked when the widget gains input focus. :param on_lose_focus: A handler that will be invoked when the widget loses input focus. :param validators: A list of validators to run on the value of the input. :param kwargs: Initial style properties.
Source code in core/src/toga/widgets/textinput.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
Bases: Protocol
Source code in core/src/toga/widgets/textinput.py
12 13 14 15 16 17 18 | |
__call__(widget, **kwargs)
¶
A handler to invoke when the text input is changed.
:param widget: The TextInput that was changed. :param kwargs: Ensures compatibility with arguments added in future versions.
Source code in core/src/toga/widgets/textinput.py
13 14 15 16 17 18 | |
Bases: Protocol
Source code in core/src/toga/widgets/textinput.py
21 22 23 24 25 26 27 | |
__call__(widget, **kwargs)
¶
A handler to invoke when the text input is confirmed.
:param widget: The TextInput that was confirmed. :param kwargs: Ensures compatibility with arguments added in future versions.
Source code in core/src/toga/widgets/textinput.py
22 23 24 25 26 27 | |
Bases: Protocol
Source code in core/src/toga/widgets/textinput.py
30 31 32 33 34 35 36 | |
__call__(widget, **kwargs)
¶
A handler to invoke when the text input gains focus.
:param widget: The TextInput that gained focus. :param kwargs: Ensures compatibility with arguments added in future versions.
Source code in core/src/toga/widgets/textinput.py
31 32 33 34 35 36 | |
Bases: Protocol
Source code in core/src/toga/widgets/textinput.py
39 40 41 42 43 44 45 | |
__call__(widget, **kwargs)
¶
A handler to invoke when the text input loses focus.
:param widget: The TextInput that lost focus. :param kwargs: Ensures compatibility with arguments added in future versions.
Source code in core/src/toga/widgets/textinput.py
40 41 42 43 44 45 | |