pythonpython-3.xkivykivy-languagekivymd

How can I make round text input in Kivy?


Consider:

This is my output image

I have created a custom rounded text input box using Kivy/KivyMD. The text input box has a rounded background, and I've added canvas instructions to achieve this effect. However, I'm facing two specific issues:

1.Inability to Type: When I interact with the rounded text input, I cannot type any text into it. It appears to be unresponsive to keyboard input.

2.Text Input Behind Round Rectangle: Additionally, the text input appears to be positioned behind the round rectangle. I want the text input to be in the foreground, allowing me to enter text while still retaining the rounded background.

My Kivy code

  MDFloatLayout:

    orientation: "vertical"

    size: root.width,root.height

    MDCard:

        size_hint: (None,None)

        size: (650,300)

        pos_hint:{"center_x" : 0.5 , "center_y": 0.5}

        MDBoxLayout:

            canvas.before:

                Rectangle:

                    source:"img/gradient.png"

                    size: self.size

                    pos: self.pos

            TextInput:
                hint_text: "text"

                hint_text_color: 0,0,0,1

                foreground_color:0,0,0,1

                background_color: 1,1,1,1

                multiline: False

                size_hint: (None,None)

                width: 500

                height: 30

                pos_hint:{"center_y": 0.90}

                canvas.before:
                    Color:
                        rgba: 1,1,1,1[![enter image description here](https://i.sstatic.net/z5bsJ.png)](https://i.sstatic.net/z5bsJ.png)

                    RoundedRectangle:
                        size: self.size

                        pos: self.pos

                        radius: 10,

What I tried:

I attempted to create a custom KivyMD text input widget with rounded corners by adding canvas instructions for a rounded background. I followed the guidance I found in the Kivy documentation and some online tutorials.

What I was expecting:

I expected to see a rounded text input box where I could type text. The rounded background should have a transparent or white fill, and the text input should be visible and functional.


Solution

  • You can redefine the canvas instructions that are used for the TextInput by defining your own custom widget that is based on TextInput. For example:

    <-MyTI@TextInput>:
        canvas.before:
            Color:
                rgba: self.background_color
            RoundedRectangle:
                pos: self.pos
                size: self.size
                radius: [10, 10, 10, 10]
            Color:
                rgba:
                    self.disabled_foreground_color if self.disabled else (self.hint_text_color if not self.text else self.foreground_color)
    

    The above defines a MyTI widget that is a TextInput with rounded corners. The - means that the existing kv rules for TextInput should be ignored. This is a somewhat simplified version of the kv rules for a TextInput, so there may be some capabilities that are not supported.