Python:在表单中添加验证 [英] Python : add validation in form
本文介绍了Python:在表单中添加验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能告诉我如何在 textInput
上设置 required
验证
什么时候点击Ok
按钮?
这时点击 Ok
按钮然后我检查每个 TextInput
像这样.if self.name.text.strip() == "":
但是如果我的表单有超过 50
字段,那么代码会很长?有人可以告诉我另一种在 TextInput
required 验证的简短方法>.
Can someone tell me that how to set required
validation on textInput
when click on Ok
button?
At this time click on Ok
button then i check every TextInput
like that.
if self.name.text.strip() == "":
But if my form have more than 50
feilds then it will be very lengthy code?Can someone tell me another short way to set required
validation on TextInput
.
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
Window.size = (500, 330)
class FloatInput(TextInput):
def __init__(self, **kwargs):
super(FloatInput, self).__init__(**kwargs)
def on_text(self, instance, text):
if text !="":
print(text)
class TestScreen(Screen):
name = ObjectProperty(None)
clas = ObjectProperty(None)
def check_validation(self):
if self.name.text.strip() == "":
print("Name is blank")
elif self.clas.text.strip() == "":
print("clas is blank")
class Test(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
TestScreen:
name:name
clas:clas
GridLayout:
cols: 2
padding : 30,30
spacing: 10, 10
row_default_height: '40dp'
Label:
text: 'Name'
FloatInput:
id: name
Label:
text: 'Class'
FloatInput:
id: clas
Button:
text: 'Ok'
on_release: root.check_validation()
Button:
text: 'Cancel'
推荐答案
- 创建一个GridLayout 的ScrollView
- 使用循环创建由 Label 和 TextInput 小部件组成的表单的每一行
- 使用循环访问 self.container.children 中的子小部件.
- Create a ScrollView of GridLayout
- Use a loop to create each line of the form consisting of a Label and TextInput widgets
- Use a loop to access child widget in self.container.children.
详情请参考以下示例.
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.metrics import dp
from kivy.properties import ObjectProperty, DictProperty
Window.size = (500, 330)
class FloatInput(TextInput):
def on_text(self, instance, text):
if text != "":
print("id={0}, text={1}".format(instance.id, text))
class TestScreen(Screen):
container = ObjectProperty(None)
def __init__(self, **kwargs):
super(TestScreen, self).__init__(**kwargs)
self.container.bind(minimum_height=self.container.setter('height'))
self.create_form()
def create_form(self):
# create 50 labels and text inputs
for i in range(50):
label = Label(text="Label {}".format(i), size_hint_y=None, height=dp(40))
text_input = FloatInput(id=str(i), hint_text="TextInput {}".format(i), size_hint_y=None, height=dp(40))
self.container.add_widget(label)
self.container.add_widget(text_input)
def check_validation(self):
for child in reversed(self.container.children):
if isinstance(child, Label):
label_text = child.text
if isinstance(child, FloatInput):
if child.text.strip() == "":
print("{0} - TextInput is blank, obj={1}".format(label_text, child))
class Test(App):
def build(self):
return TestScreen()
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
<TestScreen>:
container: container
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
ScrollView:
GridLayout:
id: container
cols: 2
size_hint_y: None
padding : 30,30
spacing: 10, 10
BoxLayout:
size_hint_y: 0.2
Button:
text: 'Ok'
on_release: root.check_validation()
Button:
text: 'Cancel'
输出
这篇关于Python:在表单中添加验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文