KivyMD如何从KivyMD文本字段获取和更改文本

时间:2020-07-14 05:20:35

标签: python kivy

class MainWindow(Screen):
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        gang = ObjectProperty(None)

        gang.text = 'Button'
class SecondWindow(Screen):
    def __init__(self, **kwargs):
        super(SecondWindow, self).__init__(**kwargs)      
        
        
Test().run()

还有KV代码

WindowManager:
    MainWindow:
    SecondWindow:

<MainWindow>:
    gang:gang
    MDFillRoundFlatButton
        
        text:"Validate"
        size_hint:0.3, None
        pos_hint: {'center_x': 0.5, 'top':0.2}
    MDTextFieldRound
        id:gang
        hint_text:"Enter Your License Key"
        multiline:False
        size_hint: (0.7, None)
        pos_hint: {"center_x": .5, "center_y": .5}
        halign:"center"
<SecondWindow>:
    name: "second"
    on_enter:app.file_manager_open()

每当我运行此命令时,我都会得到输出'AttributeError:'kivy.properties.ObjectProperty'对象没有属性'text''我以某种方式猜测object属性没有同步,我很抱歉代码超级搞砸了。

1 个答案:

答案 0 :(得分:0)

在类中定义Property时,它必须在任何方法之外。尝试更改:

class MainWindow(Screen):
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        gang = ObjectProperty(None)

        gang.text = 'Button'

收件人:

class MainWindow(Screen):
    gang = ObjectProperty(None)

    def on_kv_post(self, base_widget):
        self.gang.text = 'Button'

这将正确定义ObjectProperty,并延迟text属性的设置,直到设置了ObjectProperty