dd619
dd619

Reputation: 6170

Python for kivy:Pass values between multiple screens

I am developing a kivy app in which ,there are two screens 1.LoginScreen and 2.HomeScreen.

What required is - A value 'xyz' which is computed in class LoginScreen, to be passed to the method 'insertdata' of class HomeScreen and want to display that value on a label.

For this I tried following code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen, ScreenManager


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

    def auth(self):
        xyz=1
        self.manager.current="home"
        obj=HomeScreen()
        # 1. To Pass 'xyz' to method scrn2 
        HomeScreen.insertdata(obj)


class HomeScreen(Screen):

    def __init__(self,**kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        if (a==1):
            # 2. To display label
        self.scrn2()

    def insertdata(self):
        print "screen 2"
        label=Label(text="good moring")
        self.add_widget(label)


class ScreenApp(App):
    pass

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

Here:

1)the 1st way is proper , as it is passing 'xyz' and calling the method insertdata but it dosen't display label

2) in 1st way I have to create to create an object of HomeScreen ,to call insertdata, which in turn calls the ___init__ of Homescreen and init calls insertdata

1) It loads data before user authentication at loginscreen

insertdata gets total 3 calls, which is affecting app launch time.

Suggest me any simple and effective way for this. Thanks in advance.

Upvotes: 4

Views: 6332

Answers (1)

Nykakin
Nykakin

Reputation: 8747

You can use Screen.manager() method to get an manager object from one screen, use it to retrieve another one with its ScreenManager.get_screen() method and then pass the value directly. For an working example check an answer of this question: Kivy - Slider Class value change on another screen

Upvotes: 7

Related Questions