Python | TextInput in kivy using .kv file

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

TextInput:

The TextInput widget provides a box for editable plain text. Unicode, multiline, cursor navigation, selection and clipboard features are supported.

The TextInput uses two different coordinate systems:



  • (x, y) – coordinates in pixels, mostly used for rendering on screen.
  • (row, col) – cursor index in characters / lines, used for selection and cursor movement.
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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to Show how to use textinput 
# (UX widget) in kivy using .kv file
  
# import kivy module    
import kivy  
       
# base Class of your App inherits from the App class.    
# app:always refers to the instance of your application   
from kivy.app import App 
     
# this restrict the kivy version i.e  
# below this kivy version you cannot  
# use the app or software  
kivy.require('1.9.0')
  
# Widgets are elements
# of a graphical user interface
# that form part of the User Experience.
from kivy.uix.widget import Widget
  
# The TextInput widget provides a
# box for editable 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 the widget class
class textinp(Widget):
    pass
  
# Create the app class
class MainApp(App):
  
    # Building text input
    def build(self):
        return textinp()
  
    # Arranging that what you write will be shown to you
    # in IDLE
    def process(self):
        text = self.root.ids.input.text
        print(text)
  
# Run the App
if __name__ == "__main__":
    MainApp().run()

chevron_right


# main.kv file

filter_none

edit
close

play_arrow

link
brightness_4
code

# .kv file implementation of the code
  
<textinp>:
    title: 'InputDialog'
    auto_dismiss: False
    id: test1
  
    # Using relative layout to arrange properly
    RelativeLayout:
        orientation: 'vertical'
        pos: self.pos
        size: root.size
        id: test2
  
        # Defining text input in .kv
        # And giving it the look . 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()

chevron_right


Output:

When you run the App you will see:

After some input you will see:



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.