mcastle
mcastle

Reputation: 3002

Changing Kivy widget attribute from another widget

I have a Screen widget with a button (with id: display_name) that has a text attribute. When I press that button, a modal is displayed that has a Text Input widget and a Button widget. I want to enter text into the modal's Text Input widget and display that text in the Screen widget's button when I press the modal's button. I am having difficulty changing the Screen button's text attribute from the modal. How do I do this? I've tried the code below, but get this error:

AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'

kv code

<ProfileScreen>:
    display_name: display_name
    GeneralBoxLayout:
        BoxLayout:
            GridLayout:
                BackButton:
                    on_release: root.manager.current = 'home'
                    size_hint: (0.1,1.0)
                Image:
                    size_hint: (0.1,1.0)
                Image:
                    source: 'img/logo4.png'
                    size_hint: (0.60,1.0)
                Image:
                    size_hint: (0.05,1.0)
                MenuButton:
                    size_hint: (0.15,1.0)
                    on_release: app.build_profile_screen(); root.manager.current = 'profile'
        BoxLayout:
            ScrollView:
                size_hint: (1,.93)
                GridLayout:
                    BoxLayout:
                        Button:
                            id: display_name
                            font_size: '14sp'
                            text_size: (290, 40)
                            halign: 'left'
                            background_normal: 'img/white_button1.png'
                            background_down: 'img/white_button1.png'
                            border: 20,20,20,20
                            markup: True
                            on_release: app.update_display_name_popup()
<UpdateDisplayNamePopup>:
    updated_display_name: updated_display_name
    size_hint: .5, .3
    BoxLayout:
        padding: [10,10,10,10]
        orientation: 'vertical'
        TextInput:
            id: updated_display_name
            hint_text: 'Enter New Display Name'
        Button:
            font_size: '14sp'
            text: 'Update Display Name'
            background_normal: 'img/green_button5.png'
            background_down: 'img/green_button5.png'
            size_hint_y: None
            on_release: root.update_display_name(); root.dismiss()

main.py code

from kivy.properties import ObjectProperty

class ProfileScreen(Screen):
    display_name = ObjectProperty(None)


class UpdateDisplayNamePopup(ModalView):
    def update_display_name(self):
        ProfileScreen.display_name.text = self.updated_display_name.text

Upvotes: 2

Views: 1722

Answers (1)

mcastle
mcastle

Reputation: 3002

I solved this by moving the update_display_name method to the ProfileScreen class, calling it from the modal's button, and passing updated_display_name.text to the method, as follows:

main.py

class ProfileScreen(Screen):
    def update_display_name(self, updated_display_name):
        self.display_name.text = updated_display_name

kv file

Button:
    on_release: app.root.get_screen('profile').update_display_name(updated_display_name.text); root.dismiss()

Upvotes: 2

Related Questions