Python | TextInput in kiws using .kv file



Kivy — it is a platform independent GUI tool in Python. Since it can run on Android, IOS, Linux, Windows, etc. It is mainly used to develop Android application, but that does not mean that it cannot be used in desktop applications.

Text Input:

The TextInput widget provides a field for editing plain text. Unicode, Multiline, cursor navigation, selection and clipboard functions are supported.

TextInput uses two different coordinate systems:

  • (x, y) — coordinates in pixels, mostly used for on-screen rendering.
  • (row, column) — cursor pointer in characters / lines used to highlight and move the cursor.
  Basic Approach:  1) import kivy 2) import kivyApp 3) import widger 4) import Relativelayout 5) import textinput 6) Set minimum version (optional) 7) Create Widget class 8) Create App class 9) create .kv file (name same as the app class): 1) create textinput 10) return Layout / widget / Class (according to requirement) 11) Run an instance of the class 

Implementation of the approach

# main.py file

# Program to show how to use textinput
# (UX widget) in kiw using kv file

 
# import nodded module

import kivy 

 
# Your application base class inherits from the application class.
# app: always refers to your application instance

from kivy.app import App 

  
# this limits the kivy version ie
# below this version you cannot
# use application or software

kivy.require ( `1.9.0` )

  
# Widgets are elements
# GUI
# cat These are part of the user experience.

from kivy.uix.widget import Widget

 
# TextInput widget provides
# field for editing plain text

from kivy.uix.textinput import TextInput

 
# This layout allows you to set relative coordinates for children.

from kivy.uix.relativelayout import RelativeLayout

  
# Create widget class class textinp (Widget):

  pass

 
# Create an application class

class MainApp (App):

 

# Constructing text input

def build ( self ):

  return textinp ()

 

# The organization of what you write will be shown to you

  # inactive

  def process ( self ):

text = self . root.ids. input . text

print (text)

 
# Run the application

if __ name__ = = " __ main__ " :

  MainApp (). Run ()

 

# main.kv file

# .kv code implementation file

 
& lt; textinp & gt ;:

  title: ` InputDialog`

auto_dismiss: False

  id : test1

 

# Use relative layout to position correctly

  RelativeLayout:

  orientation: ` vertical`

pos: self . pos

size: root.size

  id : test2

 

# .kv text input detection

# And looking at this. pos and features

TextInput:

id : input

hint_text: `Enter text`

pos_hint: { `center_x` : 0.5 , `center_y` : 0.705 }

size_hint: 0.95 , 0.5

on_text: app.process ( )

Exit :

When you run the application, you will see:

After some input you will see: