Table of contents
1.
Introduction
2.
TextInput
3.
Examples
4.
Implementation
4.1.
Approach
4.2.
Code 
4.3.
Output
5.
Frequently Ask Questions
5.1.
Explain the "copy(data='')"
5.2.
Explain Backspace, Del, End, Shift + <dir> Default shortcuts?
5.3.
Explain Control + c, Control + x, Control + v, Control + a default shortcuts?
5.4.
Explain the "do_undo()".
5.5.
How to get the selected text in kivy?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

TextInput Widget in Kivy

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Widgets are interactive Python objects with a browser representation, usually in the form of a slider, textbox, or other control. The TextInput widget provides a box with editable plain text in kivy. Unicode, multiline, cursor navigation, selection, and clipboard are some of the exciting features that can be supported.

In this blog, we will learn about the concept of TextInput Widget in kivy

So, let's begin!

TextInput

The TextInput widget provides a box with editable plain text in kivy. Unicode, multiline, cursor navigation, selection, and clipboard functionality are all available.

In a multiline TextInput, the "enter" key adds a new line. Set the TextInput.multiline attribute to False to produce a singleline TextInput.

TextInput(text='Hello world', multiline=False)

TextInput employs two different coordinate systems:

  • (x, y): coordinates in pixels, commonly used for screen rendering
  • (col, row): cursor index in characters/lines, used for cursor selection and movement
     

To use Textinput, you must first import it using the command:

from kivy.uix.textinput import TextInput
You can also try this code with Online Python Compiler
Run Code

Examples

To make a multiline TextInput (“enter” adds a new line):

from kivy.uix.textinput import TextInput
textinput = TextInput(text='Coding Ninjas')
You can also try this code with Online Python Compiler
Run Code

 

Set the TextInput.multiline attribute to False to produce a singleline TextInput (the 'enter' key will defocus the TextInput and broadcast a TextInput.on_text_validate() event):

def on_enter(instance, value):
    print('User pressed enter in', instance)
    textinput = TextInput(text='Hello world', multiline=False)
    textinput.bind(on_text_validate=on_enter)
You can also try this code with Online Python Compiler
Run Code

 

The text of the textinput is saved in the TextInput.text property. When the text changes, trigger a callback:

def on_text(instance, value):
    print('The widget', instance, 'have:', value)
    
textinput = TextInput()
textinput.bind(text=on_text)
You can also try this code with Online Python Compiler
Run Code

 

We may assign the focus to a Textinput, which will highlight the input box and request keyboard focus:

textinput = TextInput(focus=True)
You can also try this code with Online Python Compiler
Run Code

 

If the 'escape' key is pushed or another widget requests the keyboard, the textinput is defocused. To be informed when the focus changes, you may attach a callback to the focus property:

def on_focus(instance, value):
    if value:
        print('User focused', instance)
    else:
        print('User defocused', instance)
        
textinput = TextInput()
textinput.bind(focus=on_focus)
You can also try this code with Online Python Compiler
Run Code

Implementation

This part of the blog talks about implementing the TextWidget in Kivy.

Approach

This section talks about the basic approach to implementing the TextInput widget in Kivy. The steps to be followed are as follows:-

  1.  Import kivy
  2.  Import kivyApp
  3.  Import Label
  4.  Import Scatter
  5.  Import Floatlayout
  6.  Import Textinput
  7.  Import BoxLayout
  8.  Run an instance of the class 
  9.  Create an App class
  10.  Return Layout/widget/Class
  11.  Set minimum version(optional)

Code 

The code below will show how to implement the TextInput Widget in Kivy.

import kivy

# App's base class is derived from the App class.  
# app: always refers to your application's instance
from kivy.app import App

# This restricts the kivy version, i.e., below this kivy version, you cannot use the app or software
kivy.require('1.9.0')

# The Label widget is for rendering text.
from kivy.uix.label import Label

# Module consists of the floatlayout to work with FloatLayout
from kivy.uix.floatlayout import FloatLayout

# Scatter is a programming language that may be used to create interactive widgets that can be translated.
# On a multitouch system, two or more fingers can be rotated and scaled.
from kivy.uix.scatter import Scatter

# A box with editable plain text is provided by the TextInput widget in    
from kivy.uix.textinput import TextInput

# BoxLayout places widgets in one of two ways: vertically, one on top of the other, or horizontally, one after the other.
from kivy.uix.boxlayout import BoxLayout

# Create the App class of TextInput Widget in kivy
class TutorialApp(App):
   def build(self):
        box = BoxLayout(orientation ='vertical')
        
        # Adding the text input of TextInput Widget in kivy
        text = TextInput(font_size = 50,
                        size_hint_y = None,
                        height = 100)
        floatLayout = FloatLayout()

        # To move the text on the screen to anywhere you want
        scatter = Scatter()
        label = Label(text ="Hello Ninja!!", font_size = 50)
        floatLayout.add_widget(scatter)
        scatter.add_widget(label)
        box.add_widget(text)
        box.add_widget(floatLayout)

        # Binding it with the label of TextInput Widget in kivy
        text.bind(text = label.setter('text'))
        return box

# Run the App of TextInput Widget in kivy
if __name__ == "__main__":
    TutorialApp().run()
You can also try this code with Online Python Compiler
Run Code

Output

After some input is given –

This program shows how to use textinput (UX widget) in kivy.

Frequently Ask Questions

Explain the "copy(data='')"

Copy the value from the argument data field to the clipboard. If the data isn't of the string type, it will be transformed into one. If no data is provided, then the current selection, if present, is copied.

Explain Backspace, Del, End, Shift + <dir> Default shortcuts?

Backspace- Delete the selection or character before the cursor.

Del- Delete the selection of characters after the cursor.

End- Move the cursor at the end of the line.

Shift + <dir>- Begin by selecting some text. Dir can be in any direction: up, down, left, or right.

Explain Control + c, Control + x, Control + v, Control + a default shortcuts?

Control + c- Copy selection

Control + x- Cut selection

Control + v- Paste clipboard content

Control + a- Select all the content

Explain the "do_undo()".

This action undoes any changes performed since the previous call to reset undo (). When the ctrl+z keys are pushed, this function is immediately invoked.

How to get the selected text in kivy?

To get the selected text in kivy, we can use the code given below:

def get_text_selected(self):
        """
        Performs search of the selected item from Web List
        Return text of selected item
        """
        return self.get_attribute_selected('text') 
You can also try this code with Online Python Compiler
Run Code

Conclusion

In this article, we have extensively discussed the TextInput Widget in KIVY. We start with a brief introduction of the TextInput Widget in KIVY and then discuss the steps to implement it.

After reading about the TextInput Widget, are you not feeling excited to read/explore more articles on the topic of Kivy? Don't worry; Coding Ninjas has you covered. To learn, see the Introduction to KivyThe drop-down List In KivyWidgets in Kivy, and Kivy Float Layout in Python.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass