I am trying to control a screen manager from buttons in a separate class, but I cannot figure out what to set on the button on_press:
statements.
Kivy file:
<HeaderSection>:anchor_x: 'center'anchor_y: 'top'BoxLayout:orientation: 'horizontal'size_hint: 1, .1id: headerLabel:text: 'My App'<ContentSection>:anchor_x: 'center'anchor_y: 'center'ScreenManager:size_hint: 1, .8Screen:name: 'home'Label:text: 'First screen'Screen:name: 'second'Label:text: 'Second screen'Screen:name: 'third'Label:text: 'Third screen'<FooterSection>:anchor_x: 'center'anchor_y: 'bottom'BoxLayout:orientation: 'horizontal'size_hint: 1, .1Button:text: 'first'on_press: root.ContentSection.manager.current = 'first'Button:text: 'second'on_press: root.current = 'second'Button:text: 'third'on_press: ContentSection.ScreenManager.current = 'third'
Python file:
from kivy.app import App
from kivy.lang import Builder
Builder.load_file('MyApp.kv')
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.image import Image# Declare sections
class HeaderSection(AnchorLayout):passclass ContentSection(AnchorLayout):def build(self):# Create the screen managersm = ScreenManager()sm.add_widget(FirstScreen(name='first'))sm.add_widget(SecondScreen(name='second'))sm.add_widget(ThirdScreen(name='third'))return smclass FooterSection(AnchorLayout):passclass MyAppApp(App):def build(self):#Create the sectionsfl = FloatLayout()hs = HeaderSection()cs = ContentSection()fs = FooterSection()fl.add_widget(hs)fl.add_widget(cs)fl.add_widget(fs)return flif __name__ == '__main__':MyAppApp().run()
I have tried various methods:
on_press: root.parent.ContentSection.ScreenManager.current = 'home'
on_press: root.parent.ContentSection.manager.current = 'home'
on_press: root.ContentSection.manager.current = 'home'
I feel like it is a scoping issue, errors say things like:
AttributeError: 'FooterSection' object has no attribute 'ContentSection'
So my app has the following hierarchy:
FloatLayoutHeaderSectionContentSectionScreenManagerFirstScreenSecondScreenThirdScreenFooterSectionButton for FirstScreenButton for SecondScreenButton for ThirdScreen
So I need to traverse up a level into FloatLayout, then drill down into ContentSection to access the screen manager.