How can I access widgets defined in a Screen from a different class rule in kivy?

14 views Asked by At

I have this code that has a MDFlatButton that when pressed, triggers a MDDialog widget. Below the button is a MDLabel with some random text. The MDDialog box has a title, a MDTextField and two other buttons. I want to be able to capture the text in the MDTextField, store it in a variable, like new_text, and then change the MDLabel's text to this new_text.

I have tried the code in the on_save_click method but it's not working. I thought about using the get_screen method but Content is a class rule and not a MDScreen.

class ScreenOne(MDScreen):
    pass

class ScreenThree(MDScreen):
    pass

class ScreenTwo(MDScreen):
    pass

MDScreenManager:
    ScreenOne:
    ScreenTwo:
    ScreenThree:

<ScreenOne>:
    name: "first_page"

    MDFloatLayout:
        id: settings_container
        padding: dp(10)
        md_bg_color: "white"

        MDFlatButton:
            text: "Open MDDialog"
            md_bg_color: "black"
            theme_text_color: "Custom"
            text_color: "orange"
            font_size: "15sp"
            pos_hint: {"center_x": 0.5, "center_y": .8}
            _no_ripple_effect: False
            on_release: app.show_confirmation_dialog()

        MDCard:
            id: card
            size_hint: .9, .1
            pos_hint: {"center_x": 0.5, "center_y": .3}
            radius: [20, ] * 4
            md_bg_color: "orange"

            MDLabel:
                id: label_text
                text: "Hello World"
                halign: "center"
                valign: "center"
        
<ScreenTwo>:
    name: "screen_one"
    
<ScreenThree>:
    name: "screen_two"

<Content>

    id: dialog_content
    size_hint_y: None
    height: "460dp"
    padding: ["0dp", "0dp", "0dp", "20dp"]

    MDFloatLayout:
        id: fields
        size_hint_y: None
        height: "400dp"

        pos_hint: {"center_x": 0.5, "center_y": .85}

        MDTextField:
            id: new_text
            mode: "fill"
            hint_text: "Enter new text"
            radius: [20, 20, 20, 20]
            pos_hint: {"center_x": 0.5, "center_y": 0.5}

    MDFloatLayout:
        id: save_cancel_btns
        size_hint: 1, None
        height: "60dp"
        pos_hint: {"center_x": 0.5, "center_y": .05}

        MDFlatButton:
            text: "Cancel"
            pos_hint: {"x": 0, "center_y": .5}
            _no_ripple_effect: True
            on_release: app.close_dialog()

        MDRaisedButton:
            id: save
            text: "Save"
            pos_hint: {"x": (save_cancel_btns.width - save.width)/save_cancel_btns.width , "center_y": .5}
            _no_ripple_effect: True
            on_release: 
                root.on_save_click()
                app.close_dialog()
class Content(MDFloatLayout):

    def on_save_click(self, *args):

        # get the text entered in the textfield
        textfield_entry = self.ids.new_text.text

        # trying to change ScreenOne's MDLabel's text
        self.ids.label_text.text = textfield_entry

    

class TextApp(MDApp):

    dialog = None

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

    def show_confirmation_dialog(self, *args):
        
        if not self.dialog:
            self.dialog = MDDialog(
                type = "custom",
                title = "TextField",
                radius = [35, ] * 4,
                auto_dismiss = True,
                content_cls = Content(),
            )
        self.dialog.open()

    def close_dialog(self, *args):
        self.dialog.dismiss()

if __name__ == '__main__':
    TextApp().run()
0

There are 0 answers