【问题标题】:How to write a "ghost" (or non-passable) text for a Kivy TextInput?如何为 Kivy TextInput 编写“幽灵”(或不可通过)文本?
【发布时间】:2017-01-01 00:43:45
【问题描述】:

在一个 kivy 应用程序中,我有一些文本输入小部件,我想将其标记为幻影文本。幽灵文本,我的意思是它不传递任何值给稍后要调用的号码。

例如,假设我在一个 kivy 应用中有两个文本输入条目。

TextInput:
    text: '1st number'
    id: first_id
    input_filter: 'float'
    multiline: False

TextInput:
    text: '2nd number'
    id: second_id
    input_filter: 'float'
    multiline: False

当应用程序运行时,第一个文本输入有一个默认文本“1st number”,第二个文本输入有一个默认文本“2nd number”。我将它们链接到一个函数,该函数通过一种方法将两个浮点数相加。问题是如果用户没有输入任何内容并按下“运行”按钮,它将破坏应用程序。有没有办法使文本无法传递给函数?

而且,我希望它在后台,这样用户就不必在输入数字之前点击文本输入,然后删除“第一个数字”字样,有什么办法吗?

【问题讨论】:

    标签: python-2.7 kivy kivy-language


    【解决方案1】:

    我想我已经创建了一个小示例来创建您想要的行为。我创建了一个带有 TextInputLabel 的新小部件,并且它仅在值为空时显示标签...

    a = Builder.load_string("""
    
    <FloatInput@FloatLayout>:
        empty_text: "Input a number"
        value: float(txt.text or '0.0')
        TextInput:
            id: txt
            input_filter: 'float'
    
        Label:
            center: txt.center
            size: self.parent.size or (300,300)
            text: "" if self.parent.value else self.parent.empty_text
            font_size: txt.font_size
            color: 0,0,0,1
    
    BoxLayout:
        FloatInput:
            id: fi
        Label:
            text: "%s" % fi.value
    
    """)
    
    
    
    
    class SimpleApp(App):
        def build(self):
            return a
    
    SimpleApp().run()
    

    您可以将此类用作您的输入小部件...

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 2016-07-11
      • 2018-05-16
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多