如何在使用Python中的Kivy时启用/禁用TextInput中的编辑

6

我有一段代码。(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()

kivy userInterface

1个回答

9
首先,感谢您提供一个易于操作的应用程序。
我尝试实现您想要的内容,但没有使用JSON。我正在使用简单的列表,扩展我的代码以支持JSON应该很简单。 与其使用列,我使用行这样可以更容易地将标签文本输入和复选框的属性链接在一起。 enter image description here
    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()

2
你也可以在kv文件中不使用CheckBox的on_active函数来实现这个功能。像这样:TextInput: disabled: not CheckBox.active - favcau
@favcau 不知道,谢谢提醒。我已经相应地编辑了我的答案。 - PalimPalim
亲爱的@PalimPalim,感谢您的时间。实际上还有一些问题。(1) 标签和TextInput都被编辑了,但需要仅为对应的标签编辑TextInput。(2) 编辑后,当我再次单击CheckButton时,相应的TextInput应该再次变为不可编辑状态。(3) 更改应在终端中显示(包括更改后的标签和TextInput)。 - crazyDelight
我不太明白你的意思,请分步骤重复一遍。对于第一行: 步骤1:标签显示为a,文本输入禁用显示为Goc,复选框未选中。 步骤2:复选框被取消选中 --> 文本输入可编辑。现在,标签是否应根据文本输入更改?请给我更详细的步骤说明。 - PalimPalim
分步骤说明: 第一步:标签显示为“a”,文本输入禁用显示为“Goc”,复选框未选中。 第二步:复选框被取消选中 --> 文本输入可编辑。 第三步:只有文本输入发生了变化,标签保持初始值不变。 第四步:然后在终端中显示更改(包括标签和更新的文本输入值)。 - crazyDelight

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接