This is our eight article on Python Kivy, in this article we want to learn How to Create TextInput in Kivy. 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.
Kivy Crash Course Articles
1: Kivy Crash Course Introduction & Installation
2: Introduction to Kv Design Language
5: How to Create CheckBox in Kivy
6: Kivy Image And AsyncImage Example
7: How to Create Slider in Python Kivy
Also you can watch the complete video for this article
So there are two ways that you can create TextInput in kivy, the first way is that you can create TextInput using the kivy.uix.textinput module, and the second way is using the kivy design language, so first let’s just do the first way.
This is the code for TextInput creation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from kivy.app import App from kivy.uix.textinput import TextInput class TextWindow(App): def build(self): text_input = TextInput(font_size = 50, multiline= False) return text_input if __name__ == "__main__": window = TextWindow() window.run() |
In the above code we have just imported the TextInput module from kivy.uix, and after that we have created the object of TextInput, you can see that i also specified the font size for the TextInput, also i have disabled the multiline for the TextInput.
If you run the code this will be the result
So now let’s create the TextInput using the kivy file, first of all this is my python code. in this code basically i have just created a new class that extends from the Widget class and i have returned that class in my main app class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from kivy.app import App from kivy.uix.textinput import TextInput from kivy.uix.widget import Widget class MyTextInput(Widget): pass class TextWindow(App): def build(self): #text_input = TextInput(font_size = 50, multiline= False) return MyTextInput() if __name__ == "__main__": window = TextWindow() window.run() |
Now this is our .kv file, and i have name the file textwindow.kv, make sure that this name should be similar to the main app class name, in my case it is TextWindow class.
1 2 3 4 5 6 7 8 9 |
#:kivy 1.10.0 <MyTextInput>: TextInput: width:root.width height:root.height multiline:False |
So now if you run the code this will be the result
Subscribe and Get Free Video Courses & Articles in your Email