Do you think IIT Guwahati certified course can help you in your career?
No
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
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):
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:
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:-
Import kivy
Import kivyApp
Import Label
Import Scatter
Import Floatlayout
Import Textinput
Import BoxLayout
Run an instance of the class
Create an App class
Return Layout/widget/Class
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
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
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.