Kivy - Screen Manager - access to an attribute in another class

Using Kivy Screen Manager, I create two screens. While I'm on screen 1, I want to change the shortcut on screen two. I highlight the problem area in my code:

my test.ky:

#: import ScreenManager kivy.uix.screenmanager.ScreenManager #: import Screen kivy.uix.screenmanager.ScreenManager #: import SettingsScreen screen ScreenManager: MenuScreen: SettingsScreen: <MenuScreen>: name: 'MenuScreen' BoxLayout: Button: text: 'Goto nn' on_press: root.manager.current = 'SettingsScreen' root.change_text() <SettingsScreen>: name: 'SettingsScreen' label_id: label_field BoxLayout: Label: id: label_field text: "to_be_changed" 

and my screen.py

 from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.screenmanager import ScreenManager, Screen class MenuScreen(Screen): def change_text(self): pass # HERE: something like # root.SettingsScreen.label_field.text = 'new text' class SettingsScreen(Screen): pass class TestApp(App): pass TestApp().run() 

Any help is much appreciated! Thanks Nico

+6
source share
1 answer

How about this:

When you press the button on MenuScreen, it sets an attribute for itself containing the text that you want to put in the SettingsScreen label. MenuScreen is then assigned the id value in the kv file, which is used to refer to 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 screen names and identifiers under the ScreenManager itself in the kv file, as this is what I usually did to make this work.

+11
source

Source: https://habr.com/ru/post/979287/


All Articles