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

2024-03-20

我有一段代码。 (1) TextInput 值应该显示,但首先它不应该是可编辑的,单击相应的 CheckBox 后,TextInput 将可编辑。
(2) 使用迭代,Label 和 TextInput 应该获得值。 Label 和 TextInput 的值不应该被硬编码(尽管它存在于我的代码中,@FJSevilla 帮助了我)。
(3) 但是,Label 和 TextInput 的值以 json 格式存储在变量中。像这样的东西(你可以考虑像地图中的键、值对)[variable = '{"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()

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

我尝试实现您正在寻找的内容,但 JSON 除外。我使用的是一个简单的列表,将我的代码扩展为 JSON 应该很简单。

我不使用列,而是使用行这使得将标签 textinput 和 checkbox 的属性链接在一起变得更容易。

    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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在Python中使用kivy启用/禁用TextInput编辑 的相关文章

随机推荐