Kivy: Use .get_screen() to Access Objects from Other Screens
All you need to access objects in a different screen is the .get_screen() method, the name of the screen, object id, and object property. For example, let’s say I want to reference the TextInput on my HomeScreen from a different screen.
Quick reference to HomeScreen from another screen with HomeScreen name as home_screen and HomeScreen TextInput id as text_input:
#.py file:# ---- One option ---- #self.manager.get_screen("home_screen").ids.text_input.text = "new text"# ---- Another option: ---- #reference_to_next_screen = self.manager.get_screen("home_screen")
reference_to_next_screen.ids.text_input.text = "new text"# .kv file:# replace self with root:root.manager.get_screen("home_screen).ids.text_input.text = "new"
Working code example where reference to OtherScreen is in the .kv file below the .py file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
Builder.load_file("medium.kv")
Window.clearcolor = (0, 0.6, 0.1, 1.0)
Window.size = (400, 600)
class HomeScreen(Screen):
pass
class OtherScreen(Screen):
pass
class RootWidget(ScreenManager):
pass
class MainApp(App):
def build(self):
self.title = "My Gardens App"
return RootWidget()
if __name__ == "__main__":
MainApp().run()
.kv file:
<HomeScreen>:
name: "home_screen"
GridLayout:
cols: 1
Label:
id: home_title
text: "Home Screen"
font_size: 30
TextInput:
id: your_message
hint_text: "Type your message here."
font_size: 25
Button:
text: "Go to OtherScreen"
font_size: 25
on_press: root.manager.current = "other_screen"
<OtherScreen>
name: "other_screen"
GridLayout:
cols: 1
Label:
id: search_title
text: "Title"
font_size: 25
Button:
text: "Click Here to Change Above 'Title'\nto your_message from HomeScreen"
font_size: 20 # ---- This is the reference --- #
on_press: root.ids.search_title.text = root.manager.get_screen("home_screen").ids.your_message.text Button:
text: "Go Home"
on_press: root.manager.current = "home_screen"
<RootWidget>:
HomeScreen:
name: "home_screen"
OtherScreen:
name: "other_screen"
This does the same thing as above, but references the OtherScreen from a function in the .py file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
Builder.load_file("medium.kv")
Window.clearcolor = (0, 0.6, 0.1, 1.0)
Window.size = (400, 600)
class HomeScreen(Screen):
pass
class OtherScreen(Screen):
def get_home_screen_text_input(self):
ref_to_other_screen = self.manager.get_screen("home_screen")
self.ids.other_title.text = ref_to_other_screen.ids.your_message.text
class RootWidget(ScreenManager):
pass
class MainApp(App):
def build(self):
self.title = "My Gardens App"
return RootWidget()
if __name__ == "__main__":
MainApp().run()
.kv file. There is only one change from the first .kv file — replacing on_press on OtherScreen to the function from the .py code. The change is marked:
<HomeScreen>:
name: "home_screen"
GridLayout:
cols: 1
Label:
id: home_title
text: "Home Screen"
font_size: 30
TextInput:
id: your_message
hint_text: "Type your message here."
font_size: 25
Button:
text: "Go to OtherScreen"
font_size: 25
on_press: root.manager.current = "other_screen"
<OtherScreen>
name: "other_screen"
GridLayout:
cols: 1
Label:
id: other_title
text: "Title"
font_size: 25
Button:
text: "Click Here to Change Above 'Title'\into your_message from HomeScreen"
font_size: 20 # -------- only change from the first .kv file -------- #
on_press: root.get_home_screen_text_input() Button:
text: "Go Home"
on_press: root.manager.current = "home_screen"
<RootWidget>:
HomeScreen:
name: "home_screen"
OtherScreen:
name: "other_screen"