python - Kivy 中的不可编辑文本

标签 python kivy

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

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

最佳答案

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

原文:

TextInput:
    id: _a
    text: 'Name: '

带有标签:

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'

关于python - Kivy 中的不可编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159632/

相关文章:

python - pycharm ubuntu : ImportError: No module named _clock 上的 Kivy 1.9.1

python - Kivy - InputText 的限制值

python - 如何从Kivy数据库中获取spinner中的数据

kivy - Kivy 和 Buildozer 的问题

python - 大量 Unicode 字符在 kivy TextInput 中无法正确呈现

Python Flask,上传文件 - 引发 BuildError(端点,值,方法)

python - 使用 PyQt5 将 qDebug 输出重定向到文件

python - 与类(class)相关的问题

python - Django 从 Sphinx 提供静态页面

python - 使用其他嵌入的 JSON 对象展平 JSON 对象