将数字键盘设置为我的kivy应用程序python

时间:2016-06-08 09:57:26

标签: android python keyboard kivy

我有一个textinput的kivy应用程序,我想在智能手机中显示一个数字键盘。我一直在阅读它,我认为使用属性input_type=number我可以得到正确的结果,但我意识到,随着kivy更新现在不起作用。当textinput聚焦时,我怎么能得到数字键盘?应用程序在横向模式下可能很有用,或键盘仍然需要半屏?这里有代码:

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()

1 个答案:

答案 0 :(得分:0)

我觉得有点晚了,或许有人明天会找。

你应该更改TextInput的input_type属性,例如:

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

我建议您为Android和桌面创建一个新的自定义小部件,就像实现maxdigits属性一样:

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)