繁体   English   中英

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

[英]KivyMD how to get and change text from KivyMD textfield

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()

Whenever i run this i get the output ' AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text' ' I'm guessing somehow the object property isn't syncing up, i apologize that the code is super messed up.

在 class 中定义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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM