[英]Kivy: Can the insert_text() function of TextInput be overrided in kv language?
有沒有一種方法只能使用kv語言覆蓋TextInput
的insert_text()
函數? 我想將文本限制為僅數字值。 我知道有一種方法可以通過python代碼來完成,例如我發現的以下示例:
class NumericInput(TextInput):
def insert_text(self, substring, from_undo=False):
if not from_undo:
try:
int(substring)
except ValueError:
return
super(NumericInput, self).insert_text(substring, from_undo)
但是有沒有辦法對kv文件中的TextInput小部件執行相同的操作:
<Temperature>:
name: "Temperature"
BoxLayout:
orientation: 'vertical'
Label:
text: "Farenheit"
TextInput:
id: user_input
font_size: 50
size_hint_y: None
insert_text:
如果沒有,我如何集成python代碼,以便限制TextInput
小部件的輸入文本?
您不能從kv語言覆蓋該方法。 如果要這樣做,只需在其python定義中覆蓋Temperature
小部件的方法。
TextInput還具有input_filter
屬性,可以將其設置為'int'
或'float'
以僅接受數字。 您可以在kv中設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.