I wanted to write a program that would allow using textinput and keyboard bind at the same time. But when I click on the text input widget, an error occurs. How do I rewrite the code so that it doesn't exist? (the problem is related to python kivy)
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class MyApp(App):
def build(self):
self.my_keyboard = Window.request_keyboard(self.my_keyboard_down, self.root)
self.my_keyboard.bind(on_key_down=self.my_keyboard_down)
widget = FloatLayout()
text_input = TextInput(multiline=False)
widget.add_widget(text_input)
print_text = lambda arg: print(text_input.text)
widget.add_widget(Button(on_press = print_text, size_hint = [0.2, 0.2], text = 'press pls'))
return widget
def my_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'escape': quit()
if keycode[1] == 'right': print('right')
if keycode[1] == 'left': print('left')
if __name__ == '__main__':
MyApp().run()
CodePudding user response:
For some reason, when you click on the TextInput
, the TextInput
calls your my_keyboard_down()
, but with no args. You can work around this by defining your my_keyboard_down()
with variable args:
# def my_keyboard_down(self, keyboard, keycode, text, modifiers):
def my_keyboard_down(self, *args):
if len(args) == 0:
return
else:
keyboard = args[0]
keycode = args[1]
text = args[2]
modifiers = args[3]
if keycode[1] == 'escape': quit()
if keycode[1] == 'right': print('right')
if keycode[1] == 'left': print('left')
I suspect this is a bug in the TextInput
.