【发布时间】:2018-06-29 01:50:10
【问题描述】:
我正在尝试制作只接受浮点值的输入文本。此外,输入的值必须介于两个值之间。
我创建了一个包含“验证”方法的类。如果值不在两个值之间,则会显示一个弹出窗口。
但我有一个问题。该方法仅在用户点击“Enter”时调用。我尝试在文本更改时调用该方法,但这对用户来说很烦人,因为在用户输入数据时会一直出现弹出窗口。
还有其他方法可以做这样的事情吗?
Python 文件:
class BoundedInput(BoxLayout):
value = NumericProperty()
def validate(self, min_value, max_value):
status = min_value <= self.value <= max_value
if not status:
message = f'Value must be between {min_value} and {max_value}'
popup = Popup(title='Warning', content=Label(text=message),
size_hint=(None, None), size=(300, 200))
popup.open()
Kv 文件:
<NumericInput@TextInput>:
input_filter: 'float'
multiline: False
<BoundedInput>:
orientation: 'horizontal'
Label:
text: 'Value'
NumericInput:
text: str(root.value)
on_text_validate:
root.value = float(self.text)
root.validate(5, 100)
【问题讨论】:
标签: python kivy kivy-language