Home > Mobile >  how do i share a variable in the app class to diffrent Screens in screen manager in kivy with python
how do i share a variable in the app class to diffrent Screens in screen manager in kivy with python

Time:07-18

I'm trying to create a simple app that asks for a user name in the first few pages. I have a variable in my testApp class that I want to set and get from other classes inheriting the (Screen). I've tried everything. I've tried using self.root, self.parent, self.manager, self.manager.root/self . nothing seems to be working

here is the code in main.py:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import Clock
from kivy.properties import StringProperty


class MainMenu(Screen):
    pass

class secondMenu(Screen):
    def __init__(self,**kwargs):
        super(secondMenu, self).__init__(**kwargs)

    def set_name(self,field):
        self.manager.parent.name = field.text

class thirdMenu(Screen):
    def __init__(self,**kwargs):
        super(thirdMenu, self).__init__(**kwargs)
        Clock.schedule_interval(self.update,1/60)
    
    def update(self,_):
        pass

class testApp(App):
    name = StringProperty('')


    def __init__(self,**kwargs):
        super(testApp, self).__init__(**kwargs)
        Clock.schedule_interval(self.update,1/60)

    def build(self):
        sm = ScreenManager()
        sm.add_widget(MainMenu(name='main'))
        sm.add_widget(secondMenu(name='second'))
        sm.add_widget(thirdMenu(name='third'))
        return sm
    def update(self,dt):
        pass
    def on_name(self):
        print(self.name)

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

and here is the kv file:

MainMenu:


<MainMenu>:
    canvas.before:
        Color:
            rgb:(.3,.6,.7)
        Rectangle:
            size: self.size
    GridLayout:
        cols: 1
        Label:
            text: 'lading page'
            color:(.30,.2,.9)
        Button:
            text:'go to next page'
            on_press: root.manager.current = 'second'

<secondMenu>:
    canvas.before:
        Color:
            rgb:(.3,.6,.7)
        Rectangle:
            size: self.size
    GridLayout:
        cols: 1
        Label:
            text: 'enter info'
            color:(.30,.2,.9)
        TextInput:
            id:input_name
            text:'enter your name'
            on_text_validate: root.set_name(self)
            multiline: False
        Button:
            text:'go to next page'
            on_press: 
                root.manager.current = 'third'
                root.manager.transition.direction = 'up'


<thirdMenu>:
    canvas.before:
        Color:
            rgb:(.3,.6,.7)
        Rectangle:
            size: self.size
    GridLayout:
        cols: 1
        Label:
            id:label_name
            text: ''
            color:(.30,.2,.9)

I'm just pretty bad at OOP in general, but I managed to solve most of my problems related to that. now that I'm learning about the screen manager, I don't know what to do anymore

CodePudding user response:

According to this answer and also Kivy documentation, you need to use ScreenManager class for exchanging variables between Screens .

CodePudding user response:

Since your name property is in the App, you can set it like this:

def set_name(self, field):
    App.get_running_app().name = field.text
    # self.manager.parent.name = field.text

Note that using on_text_validate: to trigger this method means that you must hit Enter when you type in the user name.

  • Related