Skip to content Skip to sidebar Skip to footer

Python - Kivy - Builtdozer : Having A Button Call The Android Keyboard

Framework: I am talking about an Android app written in Python 2.7 and packaged with Builtdozer I have a Builder inside the app with a button Builder.load_string(''' FloatLayout:

Solution 1:

raw_input allows to get input from stdin (terminal). On Android you will not have the terminal available. In addition, raw_input is blocking, this causes the main event loop of your app to be freeze and will cause your app to stop responding.

You shouldn't use raw_input but Kivy's own methods.

On the other hand, you want to make your button editable (as if it were a TextInput). You can create your own custom Button class or use WindowBase.request_keyboard() to request the keyboard manually. However, you can do a little trick by hiding a TextInput and use it to enter the text:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput

kv_text= ('''
<MyWidget>:
    FloatLayout:
        orientation: 'horizontal'
        Button:
            text: 'Hello'
            font_size: '20sp'
            pos_hint: {'x':.0, 'y':.3}
            size_hint: .4, .8
            on_release: root.change_name(self)

        Button:
            text: 'World'
            font_size: '20sp'
            pos_hint: {'x':0.6, 'y':.3}
            size_hint: .4, 0.8
            on_release: root.change_name(self)
''')


classMyWidget(FloatLayout):

    def__init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
        self.hide_input = TextInput(size_hint=(None,  None),
                                    size = (0, 0),
                                    multiline = False)
        self.hide_input_bind = Nonedefchange_name(self,  instance):
        if self.hide_input_bind:
            self.hide_input.unbind_uid('text',  self.hide_input_bind)

        self.hide_input.text = instance.text
        self.hide_input.focus = True
        self.hide_input_bind = self.hide_input.fbind('text',  self._update_text,  instance)

    def_update_text(self, button,  instance,  value):
        button.text = value



classMyKivyApp(App):
    defbuild(self):
        return MyWidget()

defmain():
    Builder.load_string(kv_text)
    app = MyKivyApp()
    app.run()

if __name__ == '__main__':
    main()

App working on Android (Kivy Launcher):

enter image description here

Post a Comment for "Python - Kivy - Builtdozer : Having A Button Call The Android Keyboard"