[파이썬] Kivy `TextInput` 위젯

The Kivy framework is a powerful tool for creating cross-platform applications with a beautiful and responsive user interface. One of the key elements in building user interfaces is accepting user input. The TextInput widget in Kivy allows users to enter text through a keyboard or other input devices.

In this blog post, we will explore how to use the TextInput widget in Kivy to create dynamic and interactive user interfaces.

Installation

To get started with Kivy, you need to install it on your system. You can install Kivy using pip:

pip install kivy

Creating a Basic TextInput Widget

Let’s start by creating a simple Kivy application with a TextInput widget. Create a new Python file, for example, main.py, and open it in your code editor.

Import the required modules:

from kivy.app import App
from kivy.uix.textinput import TextInput

Now, create a Kivy application class inheriting from App:

class MyApp(App):
    def build(self):
        # Create a TextInput widget
        text_input = TextInput()

        return text_input

In the build method of the application class, we create a TextInput widget and return it as the root widget of the application.

Finally, run the application:

MyApp().run()

You should see a new window with an empty TextInput widget. You can now type text in the widget using your keyboard.

Handling User Input

To retrieve the user input from the TextInput widget, we can bind it to a callback function. This function will be called whenever the text inside the TextInput changes.

Let’s modify our previous example to print the user input as it is being typed:

class MyApp(App):
    def build(self):
        # Create a TextInput widget
        text_input = TextInput()

        # Bind the on_text event to a callback function
        text_input.bind(text=self.on_text)

        return text_input

    def on_text(self, instance, value):
        print(value)

In the on_text method, we receive the TextInput instance and the new value of the text. We simply print the value to the console.

Now, when you run the application and type text in the TextInput widget, it will be printed in the console.

Formatting and Manipulating Text

The TextInput widget in Kivy provides a range of methods and properties to format and manipulate the entered text. You can change the font, size, color, and other attributes of the text. You can also select portions of the text and apply various formatting options.

Here’s an example of changing the font size and color of the text:

class MyApp(App):
    def build(self):
        # Create a TextInput widget
        text_input = TextInput(text='Hello, World!', font_size=20, foreground_color=(1,0,0,1))

        return text_input

In this example, we set the initial text of the TextInput widget to ‘Hello, World!’, change the font size to 20 pixels, and set the foreground color to red.

Feel free to experiment with other text formatting options and explore the full documentation of the TextInput widget to unleash its full potential.

Conclusion

In this blog post, we have learned how to use the TextInput widget in Kivy to create interactive text input fields in our applications. We started by creating a basic TextInput widget, handling user input, and explored text formatting options.

The TextInput widget is just one of many powerful UI elements available in the Kivy framework. With this knowledge, you can now create dynamic and responsive user interfaces in your Python applications.

Happy coding!