How do I set Id of widget inside python file?

Viewed 47

I am trying to change the text of an MDLabel inside one screen from another screen. I can reference the screen itself but since I'm not using kv language I can't set Id of the MDLabel I'm trying to reference.

Is there a way to set Id of an MDLabel from within Python and add it to self.ids of the screen it's part of? -Or. Is there another way to reference widgets of another screen?

My code:

def main():
    class HomeScreen(Screen, GridLayout, MDApp):
        def __init__(self, **kwargs):
            super(HomeScreen, self).__init__(**kwargs)

            self.add_widget(MDRaisedButton(text='Read', size_hint=(.3, .2), font_size='30sp', on_press=lambda x:self.changerReadMail()))

        def changerReadMail(self, *args):
            self.manager.transition.direction = 'right'
            # It's here I want to change the text of label inside the ReadMail class.
            self.manager.current = 'read'

    class ReadMail(Screen, FloatLayout, MDApp):
        def __init__(self, **kwargs):
            super(ReadMail, self).__init__(**kwargs)
            label = (MDLabel(text='hej'))
            self.add_widget(label)
            self.add_widget(MDFillRoundFlatButton(text='Back', font_size='20sp', size_hint=(.1,.1), pos_hint={'x':.01, 'y':.02}, on_press=lambda x:self.changerInbox()))   

        def changerInbox(self, *args):
            self.manager.transition.direction = 'left'
            self.manager.current = 'home'

    class KivyApp(MDApp): 
        def build(self):
            Window.size = (1000, 600)
            self.sm = ScreenManager()
           
            self.sm.add_widget(HomeScreen(name='home'))
            self.sm.add_widget(ReadMail(name='read'))

            self.sm.current = 'home'
            return self.sm
        
    KivyApp().run()


if __name__ == '__main__':
    main()

1 Answers

I solved this one!

Here's my solution (See the line commented with "This is a new line"):

import weakref #This is a new line

def main():
    class HomeScreen(Screen, GridLayout, MDApp):
        def __init__(self, **kwargs):
            super(HomeScreen, self).__init__(**kwargs)

            self.add_widget(MDRaisedButton(text='Read', size_hint=(.3, .2), font_size='30sp', on_press=lambda x:self.changerReadMail()))

        def changerReadMail(self, *args):
            self.manager.transition.direction = 'right'
            self.manager.get_screen('read').ids.test.text = 'test' #This is a new line
            self.manager.current = 'read'

    class ReadMail(Screen, FloatLayout, MDApp):
        def __init__(self, **kwargs):
            super(ReadMail, self).__init__(**kwargs)
            label = (MDLabel())
            self.ids['test'] = weakref.ref(label) #This is a new line
            self.add_widget(label)
            self.add_widget(MDFillRoundFlatButton(text='Back', font_size='20sp', size_hint=(.1,.1), pos_hint={'x':.01, 'y':.02}, on_press=lambda x:self.changerInbox()))   

        def changerInbox(self, *args):
            self.manager.transition.direction = 'left'
            self.manager.current = 'home'

    class KivyApp(MDApp): 
        def build(self):
            Window.size = (1000, 600)
            self.sm = ScreenManager()
           
            self.sm.add_widget(HomeScreen(name='home'))
            self.sm.add_widget(ReadMail(name='read'))

            self.sm.current = 'home'
            return self.sm
        
    KivyApp().run()


if __name__ == '__main__':
    main()

Related