TextField#
See also Text fields let users enter and edit text. KivyMD provides the following field classes for use: Note To display an error in a text field when using the
Note Warning While there is no way to change the color of the border. See also See more information in the
MDTextField inherited from
TextInput. Therefore, most parameters and all
events of the TextInput class are also
available in the MDTextField class.MDTextField#
MDTextField can be with helper text and without.Without helper text mode#
MDTextField:
hint_text: "No helper text"
Helper text mode on
on_focus event#MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
Persistent helper text mode#
MDTextField:
hint_text: "Persistent helper text"
helper_text: "Text is always here"
helper_text_mode: "persistent"
Helper text mode ‘on_error’#
helper_text_mode: "on_error" parameter, set the “error” text field
parameter to True:from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
BoxLayout:
padding: "10dp"
MDTextField:
id: text_field_error
hint_text: "Helper text on error (press 'Enter')"
helper_text: "There will always be a mistake"
helper_text_mode: "on_error"
pos_hint: {"center_y": .5}
'''
class Test(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(KV)
def build(self):
self.screen.ids.text_field_error.bind(
on_text_validate=self.set_error_message,
on_focus=self.set_error_message,
)
return self.screen
def set_error_message(self, instance_textfield):
self.screen.ids.text_field_error.error = True
Test().run()
Helper text mode ‘on_error’ (with required)#
MDTextField:
hint_text: "required = True"
required: True
helper_text_mode: "on_error"
helper_text: "Enter text"
Text length control#
MDTextField:
hint_text: "Max text length = 5"
max_text_length: 5
Multi line text#
MDTextField:
multiline: True
hint_text: "Multi-line text"
Rectangle mode#
MDTextField:
hint_text: "Rectangle mode"
mode: "rectangle"
Fill mode#
MDTextField:
hint_text: "Fill mode"
mode: "fill"
Round mode#
MDTextField:
hint_text: "Round mode"
mode: "round"
max_text_length: 15
helper_text: "Massage"
MDTextFieldRect#
MDTextFieldRect inherited from
TextInput. You can use all parameters and
attributes of the TextInput class in the
MDTextFieldRect class.MDTextFieldRect:
size_hint: 1, None
height: "30dp"
Clickable icon for MDTextField#
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivymd.app import MDApp
from kivymd.uix.relativelayout import MDRelativeLayout
KV = '''
<ClickableTextFieldRound>:
size_hint_y: None
height: text_field.height
MDTextField:
id: text_field
hint_text: root.hint_text
text: root.text
password: True
icon_left: "key-variant"
MDIconButton:
icon: "eye-off"
pos_hint: {"center_y": .5}
pos: text_field.width - self.width + dp(8), 0
theme_text_color: "Hint"
on_release:
self.icon = "eye" if self.icon == "eye-off" else "eye-off"
text_field.password = False if text_field.password is True else True
MDScreen:
ClickableTextFieldRound:
size_hint_x: None
width: "300dp"
hint_text: "Password"
pos_hint: {"center_x": .5, "center_y": .5}
'''
class ClickableTextFieldRound(MDRelativeLayout):
text = StringProperty()
hint_text = StringProperty()
# Here specify the required parameters for MDTextFieldRound:
# [...]
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
Test().run()
MDTextFieldRect class.
API - kivymd.uix.textfield.textfield#
- class kivymd.uix.textfield.textfield.MDTextFieldRect(**kwargs)#
TextInput class. See module documentation for more information.
- Events:
- on_text_validate
Fired only in multiline=False mode when the user hits ‘enter’. This will also unfocus the textinput.
- on_double_tap
Fired when a double tap happens in the text input. The default behavior selects the text around the cursor position. More info at
on_double_tap().- on_triple_tap
Fired when a triple tap happens in the text input. The default behavior selects the line around the cursor position. More info at
on_triple_tap().- on_quad_touch
Fired when four fingers are touching the text input. The default behavior selects the whole text. More info at
on_quad_touch().
Warning
When changing a
TextInputproperty that requires re-drawing, e.g. modifying thetext, the updates occur on the next clock cycle and not instantly. This might cause any changes to theTextInputthat occur between the modification and the next cycle to be ignored, or to use previous values. For example, after a update to thetext, changing the cursor in the same clock frame will move it using the previous text and will likely end up in an incorrect position. The solution is to schedule any updates to occur on the next clock cycle usingschedule_once().Note
Selection is cancelled when TextInput is focused. If you need to show selection when TextInput is focused, you should delay (use Clock.schedule) the call to the functions for selecting text (select_all, select_text).
Changed in version 1.10.0: background_disabled_active has been removed.
Changed in version 1.9.0:
TextInputnow inherits fromFocusBehavior.keyboard_mode,show_keyboard(),hide_keyboard(),focus(), andinput_typehave been removed since they are now inherited fromFocusBehavior.Changed in version 1.7.0: on_double_tap, on_triple_tap and on_quad_touch events added.
Changed in version 2.1.0:
keyboard_suggestionsis now inherited fromFocusBehavior.- line_anim#
If True, then text field shows animated line when on focus.
line_animis anBooleanPropertyand defaults to True.
- get_rect_instruction(self)#
- get_color_instruction(self)#
- anim_rect(self, points, alpha)#
- class kivymd.uix.textfield.textfield.MDTextField(**kwargs)#
TextInput class. See module documentation for more information.
- Events:
- on_text_validate
Fired only in multiline=False mode when the user hits ‘enter’. This will also unfocus the textinput.
- on_double_tap
Fired when a double tap happens in the text input. The default behavior selects the text around the cursor position. More info at
on_double_tap().- on_triple_tap
Fired when a triple tap happens in the text input. The default behavior selects the line around the cursor position. More info at
on_triple_tap().- on_quad_touch
Fired when four fingers are touching the text input. The default behavior selects the whole text. More info at
on_quad_touch().
Warning
When changing a
TextInputproperty that requires re-drawing, e.g. modifying thetext, the updates occur on the next clock cycle and not instantly. This might cause any changes to theTextInputthat occur between the modification and the next cycle to be ignored, or to use previous values. For example, after a update to thetext, changing the cursor in the same clock frame will move it using the previous text and will likely end up in an incorrect position. The solution is to schedule any updates to occur on the next clock cycle usingschedule_once().Note
Selection is cancelled when TextInput is focused. If you need to show selection when TextInput is focused, you should delay (use Clock.schedule) the call to the functions for selecting text (select_all, select_text).
Changed in version 1.10.0: background_disabled_active has been removed.
Changed in version 1.9.0:
TextInputnow inherits fromFocusBehavior.keyboard_mode,show_keyboard(),hide_keyboard(),focus(), andinput_typehave been removed since they are now inherited fromFocusBehavior.Changed in version 1.7.0: on_double_tap, on_triple_tap and on_quad_touch events added.
Changed in version 2.1.0:
keyboard_suggestionsis now inherited fromFocusBehavior.- helper_text#
Text for
helper_textmode.helper_textis anStringPropertyand defaults to ‘’.
- helper_text_mode#
Helper text mode. Available options are: ‘on_error’, ‘persistent’, ‘on_focus’.
helper_text_modeis anOptionPropertyand defaults to ‘none’.
- max_text_length#
Maximum allowed value of characters in a text field.
max_text_lengthis anNumericPropertyand defaults to None.
- required#
Required text. If True then the text field requires text.
requiredis anBooleanPropertyand defaults to False.
- mode#
Text field mode. Available options are: ‘line’, ‘rectangle’, ‘fill’, ‘round’.
modeis anOptionPropertyand defaults to ‘line’.
- line_color_normal#
Line color normal (static underline line) in
rgbaformat.MDTextField: hint_text: "line_color_normal" line_color_normal: 1, 0, 1, 1
line_color_normalis anColorPropertyand defaults to [0, 0, 0, 0].
- line_color_focus#
Line color focus (active underline line) in
rgbaformat.MDTextField: hint_text: "line_color_focus" line_color_focus: 0, 1, 0, 1
line_color_focusis anColorPropertyand defaults to [0, 0, 0, 0].
- line_anim#
If True, then text field shows animated underline when on focus.
line_animis anBooleanPropertyand defaults to True.
- error_color#
Error color in
rgbaformat forrequired = True.error_coloris anColorPropertyand defaults to [0, 0, 0, 0].
- fill_color_normal#
Fill background color in ‘fill’ mode when text field is out of focus.
fill_color_normalis anColorPropertyand defaults to [0, 0, 0, 0].
- fill_color_focus#
Fill background color in ‘fill’ mode when the text field has focus.
fill_color_focusis anColorPropertyand defaults to [0, 0, 0, 0].
- active_line#
Show active line or not.
active_lineis anBooleanPropertyand defaults to True.
- error#
If True, then the text field goes into
errormode.erroris anBooleanPropertyand defaults to False.
- hint_text_color_normal#
Hint text color when text field is out of focus.
New in version 1.0.0.
MDTextField: hint_text: "hint_text_color_normal" hint_text_color_normal: 0, 1, 0, 1
hint_text_color_normalis anColorPropertyand defaults to [0, 0, 0, 0].
- hint_text_color_focus#
Hint text color when the text field has focus.
New in version 1.0.0.
MDTextField: hint_text: "hint_text_color_focus" hint_text_color_focus: 0, 1, 0, 1
hint_text_color_focusis anColorPropertyand defaults to [0, 0, 0, 0].
- helper_text_color_normal#
Helper text color when text field is out of focus.
New in version 1.0.0.
MDTextField: helper_text: "helper_text_color_normal" helper_text_mode: "persistent" helper_text_color_normal: 0, 1, 0, 1
helper_text_color_normalis anColorPropertyand defaults to [0, 0, 0, 0].
- helper_text_color_focus#
Helper text color when the text field has focus.
New in version 1.0.0.
MDTextField: helper_text: "helper_text_color_focus" helper_text_mode: "persistent" helper_text_color_focus: 0, 1, 0, 1
helper_text_color_focusis anColorPropertyand defaults to [0, 0, 0, 0].
- icon_right_color_normal#
Color of right icon when text field is out of focus.
New in version 1.0.0.
MDTextField: icon_right: "language-python" hint_text: "icon_right_color_normal" icon_right_color_normal: 0, 1, 0, 1
icon_right_color_normalis anColorPropertyand defaults to [0, 0, 0, 0].
- icon_right_color_focus#
Color of right icon when the text field has focus.
New in version 1.0.0.
MDTextField: icon_right: "language-python" hint_text: "icon_right_color_focus" icon_right_color_focus: 0, 1, 0, 1
icon_right_color_focusis anColorPropertyand defaults to [0, 0, 0, 0].
- icon_left_color_normal#
Color of right icon when text field is out of focus.
New in version 1.0.0.
MDTextField: icon_right: "language-python" hint_text: "icon_right_color_normal" icon_left_color_normal: 0, 1, 0, 1
icon_left_color_normalis anColorPropertyand defaults to [0, 0, 0, 0].
- icon_left_color_focus#
Color of right icon when the text field has focus.
New in version 1.0.0.
MDTextField: icon_right: "language-python" hint_text: "icon_right_color_focus" icon_right_color_focus: 0, 1, 0, 1
icon_left_color_focusis anColorPropertyand defaults to [0, 0, 0, 0].
- max_length_text_color#
Text color of the maximum length of characters to be input.
New in version 1.0.0.
MDTextField: hint_text: "max_length_text_color" max_length_text_color: 0, 1, 0, 1 max_text_length: 5
max_length_text_coloris anColorPropertyand defaults to [0, 0, 0, 0].
- icon_right#
Right icon texture.
Note
It’s just a texture. It has no press/touch events.
icon_rightis anStringPropertyand defaults to ‘’.
- icon_left#
Left icon texture.
New in version 1.0.0.
Note
It’s just a texture. It has no press/touch events. Also note that you cannot use the left and right icons at the same time yet.
icon_leftis anStringPropertyand defaults to ‘’.
- text_color_normal#
Text color in
rgbaformat when text field is out of focus.New in version 1.0.0.
MDTextField: hint_text: "text_color_normal" text_color_normal: 0, 1, 0, 1
text_color_normalis anColorPropertyand defaults to [0, 0, 0, 0].
- text_color_focus#
Text color in
rgbaformat when text field has focus.New in version 1.0.0.
MDTextField: hint_text: "text_color_focus" text_color_focus: 0, 1, 0, 1
text_color_focusis anColorPropertyand defaults to [0, 0, 0, 0].
- font_size#
Font size of the text in pixels.
font_sizeis aNumericPropertyand defaults to ’16sp’.
- max_height#
Maximum height of the text box when multiline = True.
MDTextField: size_hint_x: .5 hint_text: "multiline=True" max_height: "200dp" mode: "fill" fill_color: 0, 0, 0, .4 multiline: True pos_hint: {"center_x": .5, "center_y": .5}
max_heightis aNumericPropertyand defaults to 0.
- radius#
The corner radius for a text field in fill mode.
radiusis aListPropertyand defaults to [10, 10, 0, 0].
- font_name_helper_text#
Font name for helper text.
font_name_helper_textis anStringPropertyand defaults to ‘Roboto’.
- font_name_hint_text#
Font name for hint text.
font_name_hint_textis anStringPropertyand defaults to ‘Roboto’.
- font_name_max_length#
Font name for max text length.
font_name_max_lengthis anStringPropertyand defaults to ‘Roboto’.
- cancel_all_animations_on_double_click(self)#
Cancels the animations of the text field when double-clicking on the text field.
- set_default_colors(self, interval: Union[float, int], updated: bool = False)#
Sets the default text field colors when initializing a text field object. Also called when the application palette changes.
- Parameters:
updated – If True - the color theme of the application has been changed. Updating the meanings of the colors.
- set_notch_rectangle(self, joining: bool = False)#
Animates a notch for the hint text in the rectangle of the text field of type rectangle.
- set_active_underline_width(self, width: Union[float, int])#
Animates the width of the active underline line.
- set_pos_hint_text(self, y: float, x: float = 12)#
Animates the x-axis width and y-axis height of the hint text.
- set_max_text_length(self)#
Called when text is entered into a text field.
- set_x_pos(self)#
- set_objects_labels(self)#
Creates labels objects for the parameters`helper_text`,`hint_text`, etc.