accessing data in the kivy language from ScreenManager

954 Views Asked by At

How can I access to the kivy data from MyScreenManager ? How can I access to Hellow or Timer data ? I cant use on_release: root.starttimer() in Hellow.

class Hellow(Screen):
    pass

class Timer(Screen):
    pass

class MyScreenManager(ScreenManager):
    def starttimer(self):
        #change text Hellow Button


root_widget = Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
MyScreenManager:
    transition: FadeTransition()
    Hellow:
    Timer:
<Hellow>:
    AnchorLayout:
        Button:
            id: but
            size_hint: None, None
            size:300,100
            text: 'make a foto'
            font_size: 30
            on_release: app.root.starttimer()

<Timer>:
    name: 'Timer'
''')



class ScreenManagerApp(App):
    def build(self):
        print(self.ids)
        return root_widget

if __name__ == '__main__':
    ScreenManagerApp().run()

some text for stackoverflow (it says that I need to type more text),

1

There are 1 best solutions below

2
On

Screen manager is only used to accept screen widgets if you try to add anything else like a button or label then it will throw an exception. kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget. Only one root object is allowed by .kv file In your case, you can access hello or Timer from each other.

<Hellow>:
    name: 'hello'
    ...
    Button:
        id: but
        ...
        on_release: root.parent.current = 'Timer'

<Timer>:
    name: 'Timer'
    Button:
        text: "Take me back to hellow"
        on_release: root.parent.current = 'hello' 

but there could be another way too.

<Main>:
    BoxLayout:
        Button:
            text: "Hello"
            on_release: sm.current = 'Timer'
            on_release: print(lbl.text)

        Button:
            text: "Timer"
            on_release: sm.current = 'Hello'
    ScreenManager:
        id: sm
        Screen:
            name: hello
            Label:
                id: lbl
                text: "I am hello"

        Screen:
            name: timer
            Label:
                text: "I am timer"

EDIT 1:

As you asked in your comment

class MyScreenManager(ScreenManager):
    def __init__(self,**kwargs):
        super(MyScreenManager,self).__init__(**kwargs)


    def starttimer(self,event):
        #print event.text
        event.text = "changed text"

<Hellow>:
    ...
    Button:
        ...
        on_release: root.parent.starttimer(self)