How can I use keyboard_on_key_down function in kivy code?

Viewed 305

I am making android app using kivy and I want to use keyboard_on_key_down function in kivy code in TextInput to call another fuction from my python file, but when I try to call it I recive that error:

https://i.stack.imgur.com/4cZ9S.png

https://i.stack.imgur.com/lAbC9.png

My goal is to print something whenever user writes anything in TextInput and my question is how should I use keyboard_on_key_down funtion in kivy code to make it work and call another function from my .py file.

Here is my .py code:

from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.widget import Widget

Builder.load_file("czar.kv")

class CzartGlos(Widget):
    def __init__(self, **kwargs):
        super(CzartGlos, self).__init__(**kwargs)
        print("Program rozpoczął działanie")
        Clock.schedule_interval(self.update, 1 / 30)

    def printer(self):
        print("PRINTER")


    def update(self, dt):
        pass

class Czar(App):
    def build(self):
        Window.clearcolor = (33 / 255, 242 / 255, 144 / 255, 1)
        return CzartGlos()

if __name__ == "__main__":
    Czar().run()

And here is important part of my .kv code:

                TextInput:
                    id: login_1
                    input_filter: "int"
                    on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
                    keyboard_on_key_down: root.printer()
                    font_name: "fonts/Baloo-Regular.ttf"
                    multiline: False
                    font_size: 32
                    padding_x:
                        [self.center[0] - self._get_text_width(max(self._lines, key=len), self.tab_width, self._label_cached) / 2.0,
                        0] if self.text else [self.center[0], 0]
                    padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
                    background_color: (0, 0, 0, 0)

I recive this error when I try to click on this TextInput.

1 Answers

The line:

keyboard_on_key_down: root.printer()

is the cause of your error message. I think you would be better off using the text property to trigger your root.printer(), like this:

    TextInput:
        id: login_1
        input_filter: "int"
        on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
#        keyboard_on_key_down: root.printer()
        on_text: root.printer()
        font_name: "fonts/Baloo-Regular.ttf"
        multiline: False
        font_size: 32
        padding_x:
            [self.center[0] - self._get_text_width(max(self._lines, key=len), self.tab_width, self._label_cached) / 2.0,
            0] if self.text else [self.center[0], 0]
        padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
        background_color: (0, 0, 0, 0)

The disadvantage is that root.printer() will be called whenever the text changes, even if it is not due to keyboard input.

Related