从另一个 class kivy 的 TextInput 获取数据

Get data from a TextInput from another class kivy

我想打印我在另一个 class 中的 TextInput 中写的内容,我有一个 MDDialog,它有一个位于另一个 class 中的 TextInput,我想要打印的是我写了但没有打印什么,好像 TextInput 的文本没有更新,请帮忙

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog

KV = '''
<Content>
    orientation: "vertical"
  
  
  

    MDTextField:
        id: t1
        hint_text: "Write"


FloatLayout:

    MDFlatButton:
        text: "PRESS"
        pos_hint: {'center_x': .5, 'center_y': .5}
        on_release: app.show_confirmation_dialog()
        
  
'''


class Content(BoxLayout):
    pass


class Example(MDApp):
    dialog = None

    def build(self):
        return Builder.load_string(KV)

    def show_confirmation_dialog(self):
        if not self.dialog:
            self.dialog = MDDialog(
                title="Example:",
                type="custom",
                auto_dismiss=False,
                content_cls=Content(),
                buttons=[   
                    MDFlatButton(
                        text="PRINT", text_color=self.theme_cls.primary_color,on_press=self.haha
                    ),
                ],
            )
        self.dialog.open()
    def haha(self,*args):
        print("Content: "+Content().ids.t1.text)
        
Example().run()

当我按下打印时,我收到:

INFO   ] [Base        ] Start application main loop
Content: 
[INFO   ] [Base        ] Leaving application in progress...
[INFO   ] [WindowSDL   ] exiting mainloop and closing.

这就是你的做法

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog

KV = '''
<Content>
    orientation: "vertical"
    MDTextField:
        id: t1
        hint_text: "Write"

FloatLayout:
    MDFlatButton:
        text: "PRESS"
        pos_hint: {'center_x': .5, 'center_y': .5}
        on_release: app.show_confirmation_dialog()
        
'''

class Content(BoxLayout):
    pass


class Example(MDApp):
    dialog = None
    def build(self):
        return Builder.load_string(KV)

    def show_confirmation_dialog(self):
        if not self.dialog:
            self.dialog = MDDialog(
                title="Example:",
                type="custom",
                auto_dismiss=False,
                content_cls=Content(),
                buttons=[   
                    MDFlatButton(
                        text="PRINT", text_color=self.theme_cls.primary_color,on_press=self.haha
                    ),
                ],
            )
        self.dialog.open()
    def haha(self,*args):
        print("Content: "+ self.dialog.content_cls.ids.t1.text)
        
Example().run()