TextField#

Text fields let users enter and edit text.

https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-fields.png

KivyMD provides the following field classes for use:

Note

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"
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-no-helper-mode.gif

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"
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-helper-mode-on-focus.gif

Persistent helper text mode#

MDTextField:
    hint_text: "Persistent helper text"
    helper_text: "Text is always here"
    helper_text_mode: "persistent"
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-helper-mode-persistent.gif

Helper text mode ‘on_error’#

To display an error in a text field when using the 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()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-helper-mode-on-error.gif

Helper text mode ‘on_error’ (with required)#

MDTextField:
    hint_text: "required = True"
    required: True
    helper_text_mode: "on_error"
    helper_text: "Enter text"
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-required.gif

Text length control#

MDTextField:
    hint_text: "Max text length = 5"
    max_text_length: 5
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-text-length.gif

Multi line text#

MDTextField:
    multiline: True
    hint_text: "Multi-line text"
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-text-multi-line.gif

Rectangle mode#

MDTextField:
    hint_text: "Rectangle mode"
    mode: "rectangle"
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-rectangle-mode.gif

Fill mode#

MDTextField:
    hint_text: "Fill mode"
    mode: "fill"
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-fill-mode.gif

Round mode#

MDTextField:
    hint_text: "Round mode"
    mode: "round"
    max_text_length: 15
    helper_text: "Massage"
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-round-mode.png

MDTextFieldRect#

Note

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"
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-rect.gif

Warning

While there is no way to change the color of the border.

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()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-clickable_right-icon.gif

See also

See more information in the 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 TextInput property that requires re-drawing, e.g. modifying the text, the updates occur on the next clock cycle and not instantly. This might cause any changes to the TextInput that occur between the modification and the next cycle to be ignored, or to use previous values. For example, after a update to the text, 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 using schedule_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: TextInput now inherits from FocusBehavior. keyboard_mode, show_keyboard(), hide_keyboard(), focus(), and input_type have been removed since they are now inherited from FocusBehavior.

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_suggestions is now inherited from FocusBehavior.

line_anim#

If True, then text field shows animated line when on focus.

line_anim is an BooleanProperty and 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 TextInput property that requires re-drawing, e.g. modifying the text, the updates occur on the next clock cycle and not instantly. This might cause any changes to the TextInput that occur between the modification and the next cycle to be ignored, or to use previous values. For example, after a update to the text, 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 using schedule_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: TextInput now inherits from FocusBehavior. keyboard_mode, show_keyboard(), hide_keyboard(), focus(), and input_type have been removed since they are now inherited from FocusBehavior.

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_suggestions is now inherited from FocusBehavior.

helper_text#

Text for helper_text mode.

helper_text is an StringProperty and defaults to ‘’.

helper_text_mode#

Helper text mode. Available options are: ‘on_error’, ‘persistent’, ‘on_focus’.

helper_text_mode is an OptionProperty and defaults to ‘none’.

max_text_length#

Maximum allowed value of characters in a text field.

max_text_length is an NumericProperty and defaults to None.

required#

Required text. If True then the text field requires text.

required is an BooleanProperty and defaults to False.

mode#

Text field mode. Available options are: ‘line’, ‘rectangle’, ‘fill’, ‘round’.

mode is an OptionProperty and defaults to ‘line’.

line_color_normal#

Line color normal (static underline line) in rgba format.

MDTextField:
    hint_text: "line_color_normal"
    line_color_normal: 1, 0, 1, 1
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-line-color-normal.gif

line_color_normal is an ColorProperty and defaults to [0, 0, 0, 0].

line_color_focus#

Line color focus (active underline line) in rgba format.

MDTextField:
    hint_text: "line_color_focus"
    line_color_focus: 0, 1, 0, 1
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-line-color-focus.gif

line_color_focus is an ColorProperty and defaults to [0, 0, 0, 0].

line_anim#

If True, then text field shows animated underline when on focus.

line_anim is an BooleanProperty and defaults to True.

error_color#

Error color in rgba format for required = True.

error_color is an ColorProperty and defaults to [0, 0, 0, 0].

fill_color_normal#

Fill background color in ‘fill’ mode when text field is out of focus.

fill_color_normal is an ColorProperty and defaults to [0, 0, 0, 0].

fill_color_focus#

Fill background color in ‘fill’ mode when the text field has focus.

fill_color_focus is an ColorProperty and defaults to [0, 0, 0, 0].

active_line#

Show active line or not.

active_line is an BooleanProperty and defaults to True.

error#

If True, then the text field goes into error mode.

error is an BooleanProperty and 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-hint-text-color-normal.gif

hint_text_color_normal is an ColorProperty and 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-hint-text-color-focus.gif

hint_text_color_focus is an ColorProperty and 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-helper-text-color-normal.png

helper_text_color_normal is an ColorProperty and 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-helper-text-color-focus.gif

helper_text_color_focus is an ColorProperty and 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-icon-right-color-normal.gif

icon_right_color_normal is an ColorProperty and 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-icon-right-color-focus.gif

icon_right_color_focus is an ColorProperty and 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-icon-right-color-normal.gif

icon_left_color_normal is an ColorProperty and 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-icon-right-color-focus.gif

icon_left_color_focus is an ColorProperty and 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-max-length-text-color.gif

max_length_text_color is an ColorProperty and defaults to [0, 0, 0, 0].

icon_right#

Right icon texture.

Note

It’s just a texture. It has no press/touch events.

icon_right is an StringProperty and 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.

https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-left-icon.png

icon_left is an StringProperty and defaults to ‘’.

text_color_normal#

Text color in rgba format 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
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-text-color-normal.gif

text_color_normal is an ColorProperty and defaults to [0, 0, 0, 0].

text_color_focus#

Text color in rgba format when text field has focus.

New in version 1.0.0.

MDTextField:
    hint_text: "text_color_focus"
    text_color_focus: 0, 1, 0, 1
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-text-color-focus.gif

text_color_focus is an ColorProperty and defaults to [0, 0, 0, 0].

font_size#

Font size of the text in pixels.

font_size is a NumericProperty and 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}
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-fill-mode-multiline-max-height.gif

max_height is a NumericProperty and defaults to 0.

radius#

The corner radius for a text field in fill mode.

radius is a ListProperty and defaults to [10, 10, 0, 0].

font_name_helper_text#

Font name for helper text.

font_name_helper_text is an StringProperty and defaults to ‘Roboto’.

font_name_hint_text#

Font name for hint text.

font_name_hint_text is an StringProperty and defaults to ‘Roboto’.

font_name_max_length#

Font name for max text length.

font_name_max_length is an StringProperty and 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_colors_to_updated(self, interval: Union[float, int])#
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_static_underline_color(self, color: list)#

Animates the color of a static underline line.

set_active_underline_color(self, color: list)#

Animates the fill color for ‘fill’ mode.

set_fill_color(self, color: list)#

Animates the color of the hint text.

set_helper_text_color(self, color: list)#

Animates the color of the hint text.

set_max_length_text_color(self, color: list)#

Animates the color of the max length text.

set_icon_right_color(self, color: list)#

Animates the color of the icon right.

set_icon_left_color(self, color: list)#

Animates the color of the icon left.

set_hint_text_color(self, focus: bool, error: bool = False)#

Animates the color of the hint text.

set_pos_hint_text(self, y: float, x: float = 12)#

Animates the x-axis width and y-axis height of the hint text.

set_hint_text_font_size(self, font_size: float)#

Animates the font size of the hint text.

set_max_text_length(self)#

Called when text is entered into a text field.

check_text(self, interval: Union[float, int])#
set_text(self, instance_text_field, text: str)#

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.

on_helper_text(self, instance_text_field, helper_text: str)#
on_focus(self, instance_text_field, focus: bool)#
on_icon_left(self, instance_text_field, icon_name: str)#
on_icon_right(self, instance_text_field, icon_name: str)#
on_disabled(self, instance_text_field, disabled_value: bool)#
on_error(self, instance_text_field, error: bool)#

Changes the primary colors of the text box to match the error value (text field is in an error state or not).

on_hint_text(self, instance_text_field, hint_text: str)#
on_width(self, instance_text_field, width: float)#

Called when the application window is resized.

on_height(self, instance_text_field, value_height: float)#
on_text_color_normal(self, instance_text_field, color: list)#
on_hint_text_color_normal(self, instance_text_field, color: list)#
on_helper_text_color_normal(self, instance_text_field, color: list)#
on_icon_right_color_normal(self, instance_text_field, color: list)#
on_line_color_normal(self, instance_text_field, color: list)#
on_max_length_text_color(self, instance_text_field, color: list)#