How can I send some text from popup to label in another class which is inherited from the Screen class?

Viewed 32

Sorry my english. How can I send some text from popup to label in another class which is inherited from the Screen class? I tried different options for accessing this object, but nothing happens. It looks like text sends to another object, because these objects has different memory adresses. I checked it. Comment indicating the problem is in the code:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.popup import Popup

Builder.load_string("""
#:import Factory kivy.factory.Factory
<Keyboard>:
    text_input: text_input
    BoxLayout:
        orientation: 'vertical'
        TextInput:
            id: text_input
        Button:
            text: 'Send text'
            on_release: root.send()

<Container>:
    label: label
    BoxLayout:
        orientation: 'vertical'
        Label:
            id: label
            text: 'Here must some be text from popup'
        Button:
            text: 'My popup'
            on_release: Factory.Keyboard().open()
        Button:
            text: 'Goto options'
            on_press: root.manager.current = 'Options'

<Options>:
    BoxLayout:
        Button:
            text: 'Back to time'
            on_press: root.manager.current = 'Time'
""")

class Options(Screen):
    def show_kv(self, instance, value):
        self.manager.current = value


class Container(Screen):
    pass


class Keyboard(Popup):
    def send(self):
        try:
            time = self.text_input.text
        except:
            time = ''
        # Here is some problem
        Container().label.text = time  # Nothing happens
        self.dismiss()


class KivyApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(Container(name='Time'))
        sm.add_widget(Options(name='Options'))
        sm.current = "Time"
        return sm


if __name__ == '__main__':
    KivyApp().run()
1 Answers

The line:

Container().label.text = time

is creating a new instance of Container, and sets the text of a Label in that new instance. However, that instance of Container is not the one that appears in your GUI. You must access the instance that is actually in your GUI. You can do that using the get_screen() method of the ScreenManager that is the root of your GUI. Like this:

    container_instance = App.get_running_app().root.get_screen('Time')
    container_instance.label.text = time
Related