2016-06-08 92 views
3

我有一個用一些textinput的kivy應用程序,我想在智能手機上顯示一個數字鍵盤。我一直在閱讀,我認爲,與財產input_type=number我可以得到正確的結果,但我意識到,與基維更新現在不工作。當我的textinput聚焦時,如何獲得數字鍵盤?使用橫向模式的應用程序可能會很有用,或者鍵盤仍然會佔用一半的屏幕?這裏你有代碼:將數字鍵盤設置爲我的kivy應用程序python

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

請給我們展示一些代碼! –

+2

@UlfGjerdingen我用代碼編輯了問題。 –

回答

0

我覺得有點晚,不知道明天有人找它。

是真的,你應該改變你的TextInput的INPUT_TYPE屬性格式,你的情況,例如:

self.add_widget(的TextInput(多=假INPUT_TYPE = '號'))

我建議你創建一個新的自定義小部件,以便在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) 
相關問題