get values from a different screen (kivy)

筅森魡賤 提交于 2019-12-04 21:12:55

You could save the shared variable in the App class, for example.
Then you can access it in kv, by writing app.label_a
Try this example:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import StringProperty

Builder.load_string("""
<Manager>:
    Screen:
        name: 'first'
        BoxLayout:
            Label:
                text: app.label_a
            Button:
                text:'go to other'
                on_press: app.sm.current = 'other'

    Screen:
        name: 'other'
        BoxLayout:
            Label:
                id: label_b
                text: "Screen 2 label. Press button to change."
            Button:
                text:'Get label text from screen 1'
                on_press: label_b.text = app.label_a    
""")


class Manager(ScreenManager):
    pass


class MyApp(App):
    label_a = StringProperty("Screen 1 Label")

    def build(self):
        self.sm = Manager()
        return self.sm    

MyApp().run()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!