Table of contents
1.
Introduction
2.
Implementation
2.1.
Approach
2.2.
Code
2.3.
Output
3.
Frequently Asked Questions
3.1.
How to create a single line TextInput?
3.2.
How to write only in capitalized letters in TextInput?
3.3.
What is the use of boxlayout in kivy?
3.4.
What sorts of input validation are there?
3.5.
What does input validation prevent?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Text Input Box with a verification button

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

Introduction

Kivy is a Python GUI(Graphical User Interface) tool that works on any platform. It can run on AndroidiOS, Linux, and Windows, among other platforms. It is mainly used to create Android applications, but it may also create desktop applications.

In this blog, we'll learn how to use the a .kv file in Python to create a button with a Text input in Kivy, similar to the input and submit buttons. To make this, we must first understand the Textinput widget and the Button in Kivy.

Implementation

Below is the step-by-step guide on implementing Text Input Box with a verification button in Kivy.

Approach

The following steps explain the basic approach to implementing a Text Input Box with a verification button.

  1. Import kivy
  2. Import kivyApp
  3. Import widget
  4. Import Boxlayout
  5. Import textinput and Button
  6. Set minimum version(optional)
  7. Create Widget class
  8. Create App class
  9. Create .kv file:
  10. Create textinput
  11. Create Button
  12. Return Layout/widget/Class(according to requirement)
  13. Run an instance of the class

Code

The code below will show how to create textinput with the button in kivy using .kv file

# Import kivy module    
import kivy  

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

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

# Widgets are graphical user interface elements included in the User Experience.
from kivy.uix.widget import Widget

# The TextInput widget from kivy.uix.textinput import TextInput provides a box for editable plain text.
from kivy.uix.textinput import TextInput

# BoxLayout organises widgets vertically, one on top of the other or horizontally, one after the other.
from kivy.uix.boxlayout import BoxLayout

# We utilise this module configuration to adjust the kivy default settings.
from kivy.config import Config
Config.set('graphics', 'resizable', True)

# Making the root widget for the.kv file
class BtnTextInput(BoxLayout):
    pass

# Textinput and btn classes are created in this class.
# in .kv file to be named main.kv
class MainApp(App):
    def build(self):

        # Returning the instance of root class
        return BtnTextInput()

# The run function executes the whole programme.
# i.e run() function that invokes the target function passed to the constructor.

if __name__ == '__main__':
    MainApp().run()
You can also try this code with Online Python Compiler
Run Code

 

.kv file implementation 

The code below is the .kv implementation of the App class.

# Creating the root widget

<BtnTextInput>:
    # To position widgets in the proper orientation, use vertical orientation to set all widgets
	# orientation: "vertical"
    # Using the Box layout
    BoxLayout:
        height: "50dp"
        size_hint_y: None

        # Creating Test input
        TextInput:
            size_hint_x: 20
            
        # Creating Button        
        Button:
            text: "Apply"
            size_hint_x: 25
            background_color: 1, 0, 0, 1
            font_size: 35

Output

Text Input Box with a verification button output

Frequently Asked Questions

How to create a single line TextInput?

Set the TextInput.multiline attribute to False to generate a singleline TextInput.

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

How to write only in capitalized letters in TextInput?

To write exclusively in capital letters:

class CapitalInput(TextInput):
    def insert_text(self, substring, from_undo=False):
        s = substring.upper()
        return super().insert_text(s, from_undo=from_undo)
You can also try this code with Online Python Compiler
Run Code

What is the use of boxlayout in kivy?

BoxLayout groups children into vertical or horizontal boxes. Position hints are only partially effective, depending on orientation: If the orientation is vertical, the following values will be used: x, right, and centre x. The following values will be used for horizontal orientation: y, top, and centre y.

What sorts of input validation are there?

Safelist validation (also known as the inclusion or positive proof) and blocklist validation are two ways to input validation (sometimes known as exclusion or negative assurance).

What does input validation prevent?

Input validation ensures that only correctly formed data enters an information system's process, preventing flawed data from remaining in the database and causing downstream components to fail.

Conclusion

In this article, we have extensively discussed the Text Input Box with a verification button in KIVY. We start with a brief introduction of the Text Input Box with a verification button in KIVY and then discuss the steps to implement it.

After reading about the Text Input Box with a verification button in KIVY, 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!

Conclusion Image

Live masterclass