Kivy - Screen Manager - Accessing attribute in other class

情到浓时终转凉″ 提交于 2019-12-01 05:22:45

How about this:

When you press the button on MenuScreen, it sets an attribute on itself containing the text you want to put in the SettingsScreen Label. Then the MenuScreen is assigned an id value in the kv file, which is used to reference this attribute. Example:

main.py

class MenuScreen(Screen):
    text = StringProperty('')
    def change_text(self):
        self.text = "The text you want to set"
        self.manager.current = "SettingsScreen"

class SettingsScreen(Screen):
    label_text = StringProperty('')

kv file

ScreenManager:
    id: screen_manager
    MenuScreen:
        id: menu_screen
        name: 'MenuScreen'
        manager: screen_manager
    SettingsScreen:
        name: 'SettingsScreen'
        manager: screen_manager
        label_text: menu_screen.text

<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Goto nn'
            on_press:
                root.change_text()

<SettingsScreen>:
    BoxLayout:
        Label:
            text: root.label_text

As you can see, I set the names and id of the screens under ScreenManager itself in the kv file, as this is what I would usually do to make this work.

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