簡體   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