Kivy中的不可编辑文本

时间:2014-04-18 18:06:54

标签: python kivy

我正在使用Kivy创建注册表单。我希望文本框中的文本在表单加载为不可编辑时出现。从下面可以看出,我可以编辑" Name"领域为" Na"。我希望用户输入名称不能退格并删除" Name"。

enter image description here

这是我的代码:

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'
""")

1 个答案:

答案 0 :(得分:3)

虽然你可能会这样做,但实施起来却很困难。我建议将该文本拆分为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'