您好我正在尝试在kivy中创建只接受某些类型字符串的文本框。第一个文本框工作正常,只接受数字,但cnuminput将接受字符串中只应该是数字的部分中的特殊字符,我不知道为什么。只是它正在发生的事情,它导致我检查int的方法和isnumeric()/ isdidgits()/ ect为一个字符串返回true,如果它应该返回false。
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.popup import Popup
def ISHOULDNTHAVETODOTHIS(st):
try:
int(st)
return True
except ValueError:
return False
else:
return False
class CNumInput(TextInput):
def insert_text(self, substring, from_undo=False):
if substring[0:3].isalpha() != True and len(self.text) < 4:
substring =(substring[0:-1])
elif substring[0:3].isalpha() == True and len(self.text) > 3 and not ISHOULDNTHAVETODOTHIS(self.text[4:]):
substring =( substring[0:-1])
elif (len(self.text) > 7):
substring = substring[0:-1]
substring = substring[0:3].upper() + substring[4:]
return super(CNumInput, self).insert_text(substring, from_undo=from_undo)
class NumInput(TextInput):
def insert_text(self, substring, from_undo=False):
if ISHOULDNTHAVETODOTHIS(substring) != True:
substring =( substring[0:-1])
popup = Popup(Title =)
elif (len(self.text) > 5):
substring = substring[0:-1]
return super(NumInput, self).insert_text(substring, from_undo=from_undo)
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 2
drop = DropDown()
self.add_widget(Label(text='Student Id'))
self.username = NumInput(multiline=False)
self.add_widget(self.username)
mainbut = Button(text='Lab', size_hint_y=None, height=20)
mainbut.bind(on_release=drop.open)
self.add_widget(Label(text='Location'))
self.add_widget(mainbut)
self.add_widget(Label(text='Course Number'))
labut = Button(text='Lab', size_hint_y=None, height=20)
clabut = Button(text='ClassRoom', size_hint_y=None, height=20)
stubut = Button(text='Studio', size_hint_y=None, height=20)
labut.bind(on_release=lambda labut: drop.select(labut.text))
clabut.bind(on_release=lambda clabut: drop.select(clabut.text))
stubut.bind(on_release=lambda stubut: drop.select(stubut.text))
drop.bind(on_select=lambda instance, x: setattr(mainbut, "text", x))
drop.add_widget(labut)
drop.add_widget(clabut)
drop.add_widget(stubut)
self.password = CNumInput(multiline=False)
self.add_widget(self.password)
class MyApp(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
MyApp().run()