如何在 python 中使用 kivy 在 TextInput 中启用/禁用编辑

cra*_*ght 5 python user-interface edit textinput kivy

我有一段代码。(1) TextInput 的值应该是显示出来的,但首先它应该是不可编辑的,点击对应的CheckBox后,TextInput是可编辑的。
(2) 使用迭代,Label 和TextInput 应该得到值。Label 和 TextInput 的值不应该被硬编码(尽管它在我的代码中,@FJSevilla 帮助我完成了这个)。
(3) 但是Label和TextInput的值是以json格式存储在一个变量中的。像这样(你可以考虑像映射中的键,值对)[变量 = '{"a" : " Goc" , "b" : "Coc", "c" : "Dow" } '](你可以看到示意图以获取更多间隙)。我很感激你的帮助。

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder

Builder.load_string("""

<Test>:
    do_default_tab: False

    TabbedPanelItem:
        text: 'page1'

        BoxLayout:
            padding: 50, 50, 50, 50
            orientation: 'horizontal'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                size_hint_x: 1
                Label:
                    text: 'a'
                Label:
                    text: 'b'
                Label:
                    text: 'c'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                TextInput:
                    text: 'Goc'
                TextInput:
                    text: 'Coc'
                TextInput:
                    text: 'Dow'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                size_hint_x: 0.40
                CheckBox: 
                    text: 'CheckBox'
                CheckBox: 
                    text: 'CheckBox'
                CheckBox: 
                    text: 'CheckBox'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                size_hint_x: 0.60
                Button:
                    text: 'save'
                Button:
                    text: 'save'
                Button:
                    text: 'save'


""")

class Test(TabbedPanel):
    pass

class MyApp(App):

    def build(self):
        test = Test()
        return test


if __name__ == '__main__':
    MyApp().run()
Run Code Online (Sandbox Code Playgroud)

kivy 用户界面

Pal*_*lim 7

首先,感谢您提供易于使用的应用程序。

我试图实现你正在寻找的东西,除了 JSON。我正在使用一个简单的列表,为 JSON 扩展我的代码应该很简单。

我没有使用列而是使用行,这样可以更轻松地将标签文本输入和复选框的属性链接在一起。

在此处输入图片说明

    from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.lang import Builder

ROWS = ['Goc', 'COC', 'EEE']

Builder.load_string("""

<Test>:
    do_default_tab: False

    TabbedPanelItem:
        text: 'page1'

        Table:
            padding: 50, 50, 50, 50
            orientation: 'vertical'

<Row>:
    spacing: 50
    #orientation: 'vertical'
    size_hint_x: 1
    txt: txtinpt.text
    Label:
        text: root.txt
    TextInput:
        id: txtinpt
        text: root.txt
        disabled: not CheckBox.active
    CheckBox:
        id:CheckBox 
        text: 'CheckBox'
        active: False
    Button:
        text: 'save'

""")
class Table(BoxLayout):
    def __init__(self, **kwargs):
        super(Table, self).__init__(**kwargs)
        for row in ROWS:
            self.add_widget(Row(row))



class Row(BoxLayout):
    txt = StringProperty()
    def __init__(self, row, **kwargs):
        super(Row, self).__init__(**kwargs)
        self.txt = row



class Test(TabbedPanel):
    pass

class MyApp(App):

    def build(self):
        test = Test()
        return test


if __name__ == '__main__':
    MyApp().run()
Run Code Online (Sandbox Code Playgroud)

  • 您也可以在没有 CheckBox:on_active 函数的情况下在 kv 中执行此操作。像这样: TextInput: disabled: not CheckBox.active (2认同)