A.Piquer
A.Piquer

Reputation: 424

Set a numeric keyboard to my kivy app python

I have a kivy app with some textinput and I want to show in my smartphone a numeric keyboard. I've been reading about it and I think that with the property input_type=number I could get the right result but I realised that with the kivy updates doesn't work nowadays. How could I get the numeric keyboard when my textinput is focused? With the app in landscape mode it could be useful or the keyboard still will take half screen? Here do you have the code:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window


class LoginScreen(GridLayout):
    def __init__(self,**kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        self.cols=2
        self.add_widget(Label(text='Subject'))
        self.add_widget(Label(text=''))
        self.add_widget(Label(text='1'))
        self.add_widget(TextInput(multiline=False))
        self.add_widget(Label(text='2'))
        self.add_widget(TextInput(multiline=False))
        self.add_widget(Label(text='3'))
        self.add_widget(TextInput(multiline=False))
        self.add_widget(Label(text='4'))
        self.add_widget(TextInput(multiline=False))
        b1=Button(text='Exit',background_color=[0,1,0,1],height=int(Window.height)/9.0) #doesn't work properly 
        self.add_widget(b1)
        b2=Button(text='Run',background_color=[0,1,0,1],height=int(Window.height)/9.0) #doesn't work properly
        self.add_widget(b2)
        b1.bind(on_press=exit) 




class SimpleKivy(App):
    def build(self):
        return LoginScreen()


if __name__=='__main__':
    SimpleKivy().run()

Upvotes: 4

Views: 1958

Answers (1)

Leonardo Rodriguez
Leonardo Rodriguez

Reputation: 34

I think is a bit late, bu maybe someone looks for it tomorrow.

Is true you should change the input_type propery of your TextInput, in your case for example:

self.add_widget(TextInput(multiline=False, input_type = 'number'))

I suggest you create a new custom widget for that in order works in android and desktop, like this that implement a maxdigits property:

class IntegerInput(TextInput):
    def __init__(self, **kwargs):
        super(IntegerInput, self).__init__(**kwargs)
        self.input_type = 'number'

    def insert_text(self, substring, from_undo=False):
        if substring.isnumeric():
            if hasattr(self, "maxdigits"):
                if len(self.text) < self.maxdigits:
                    return super(IntegerInput,self).insert_text(substring, from_undo=from_undo)
            else:
                return super(IntegerInput, self).insert_text(substring, from_undo=from_undo)

Upvotes: 1

Related Questions