【问题标题】:Non editable text in KivyKivy 中不可编辑的文本
【发布时间】:2014-04-18 18:06:53
【问题描述】:

我正在使用 Kivy 创建一个注册表单。我想要文本框中的文本,当表单加载为不可编辑时会出现。从下面可以看出,我可以将“名称”字段编辑为“Na”。我希望输入姓名的用户不能退格并删除“姓名”。

这是我的代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
import xml.etree.cElementTree as ET
import fileinput
import sys
import os


Builder.load_string("""
<Reg>:
    # This are attributes of the class Reg now
    a: _a
    b: _b
    c: _c
    d: _d
    e: _e
    f: _f
    g: _g
    result: _result
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                GridLayout:
                    cols:1
                    TextInput:
                        id: _a
                        text: 'Name: '
                    TextInput:
                        id: _b
                        text: 'Age: '
                    TextInput:
                        id: _c
                        text: 'Phone: '
                    TextInput:
                        id: _d
                        text: 'Email: '
                    TextInput:
                        id: _e
                        text: 'Address: '   
                    TextInput:
                        id: _f
                        text: 'Guardian Name: '
                    TextInput:
                        id: _g
                        text: 'Guardian Phone: '                            
                    Label:
                        id: _result

                    Button:
                        text: 'Register Me'
                        # Or you can call a method from the root class (instance of calc)
                        on_press: root.genxml(*args)
            Screen:
                name: 'screen2'
                Label: 
                    text: 'The second screen'
""")

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    虽然您可能会这样做,但实施起来会很困难。我建议将该文本拆分为Label,或使用hint_text(仅当TextInput 为空且未聚焦时才会显示该文本)。

    原文:

    TextInput:
        id: _a
        text: 'Name: '
    

    Label:

    BoxLayout:
        Label:
            size_hint_x: 0.4  # adjust as necessary, or use None and set width instead
            text: 'Name:'
        TextInput:
            id: _a
    

    hint_text:

    TextInput:
        id: _a
        hint_text: 'Name'
    

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      相关资源
      最近更新 更多