【问题标题】:how to enable/disable editing in TextInput using kivy in python如何在 python 中使用 kivy 在 TextInput 中启用/禁用编辑
【发布时间】:2017-12-27 12:17:35
【问题描述】:

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

【问题讨论】:

    标签: python user-interface kivy edit textinput


    【解决方案1】:

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

    我尝试实现您正在寻找的内容,但 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()
    

    【讨论】:

    • 您也可以在 kv 中执行此操作,而无需 CheckBox 中的功能:on_active。像这样: TextInput: disabled: not CheckBox.active
    • @favcau 不知道,谢谢。我相应地编辑了我的答案。
    • 亲爱的@PalimPalim 感谢您抽出宝贵的时间。其实有些问题还是存在的。 (1) Label 和 TextInput 都在编辑中,但只需要编辑对应 Label 的 TextInput。 (2) 编辑后,当我再次单击 CheckButton 时,对应的 TextInput 应该再次为 Non_Editable。 AND (3) 更改应显示在终端中(更改后的标签和文本输入)。
    • 我觉得我跟不上你。请按步骤重复。对于第一行: Step1:Label 显示a,Textinput disabled 显示Goc,Checkbox 未选中。 Step2 Checkox 未选中 --> TextInput 可编辑。现在标签应该根据 TextInput 改变吗?请给我更详细的步骤
    • 逐步进行: Step1:标签显示“a”,Textinput disabled 显示“Goc”,复选框未选中。第 2 步:Checkox 被取消选中 --> TextInput 可编辑。 Step3:只有TextInput改变了,Label固定在它的初始值。 Step4:然后显示终端的变化(标签和更新的TextInput值)。
    猜你喜欢
    • 2017-12-31
    • 2017-12-19
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    相关资源
    最近更新 更多