7

I would like to implement a kivy application, which has two screens (managed by a screen manager). On the first screen (called LoginScreen) there are two TextInput fields and a button. On the second screen I have two labels, which I would like to display the values, entered on the first screen. Changing the screens is done after button click.
I managed to bind these fields together so the values are displayed on the second screen. However, I would like to "process" these values in the second screen, which, unfortunately, I haven't been able to do.
By "process" I mean, that I would like to login to my e-mail account using a custom-built class (which is working) and list my unseen emails (which are provided by one of the class' function) in a kivy list.
Could, someone please tell me how I could get the entered values and use them to create my class?
I would greatly appreciate any help!
My .py file:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ListProperty, StringProperty

class MainScreenManager(ScreenManager):
    pass

class LoginScreen(Screen):
    entered_email_address = StringProperty('')
    entered_password = StringProperty('')

    def check_input(self):
        text_input_email = self.ids['ti_email'].text
        text_input_password = self.ids['ti_password'].text

        self.entered_email_address = text_input_email
        self.entered_password = text_input_password

        """
        the values in this part are printed out
        print self.manager
        print self.manager.screens
        print self.manager.get_screen('HomeScreen').email_address
        print self.manager.get_screen('HomeScreen').password
        """

        self.manager.current = 'HomeScreen'

class HomeScreen(Screen):
    email_address = StringProperty()
    password = StringProperty()

    def __init__(self, *args, **kwargs):
        super(HomeScreen, self).__init__(*args, **kwargs)

class TutorialApp(App):

    def build(self):
        return MainScreenManager()


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

My .kv file:

<MainScreenManager>:
    id: screen_manager

    LoginScreen:
        id: login_screen
        name: 'LoginScreen'
        manager: screen_manager

    HomeScreen:
        id: home_screen
        name: 'HomeScreen'
        email_address: login_screen.entered_email_address
        password: login_screen.entered_password

<LoginScreen>
    BoxLayout:
        orientation: 'vertical'
        TextInput:
            id: ti_email
            multiline: False
            size_hint_y: None
            height: 40
            font_size: 25

        TextInput:
            id: ti_password
            multiline: False
            size_hint_y: None
            height: 40
            font_size: 25

        Button:
            id: btn_login
            text: 'Login!'
            height: 100
            width: 150
            on_press:
                root.check_input()

<HomeScreen>
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: root.email_address

        Label:
            text: root.password
Bence
  • 155
  • 2
  • 3
  • 6
  • Is your question is about using a string property, or passing data between screens? Because if the second, there are plenty of examples here, which you could easily find with minimal effort. – jligeza Oct 12 '16 at 15:02
  • thanks for your reply. I added the source code. If I try to get the values out of the variables email_addess and password for example in the init method of the HomeScreen, I cannot (I wanted to print them out). In the part, which I commented out in the check_input method, I can access the values of the properties of the HomeScreen. – Bence Oct 12 '16 at 17:37
  • Check this topic: [link](http://stackoverflow.com/questions/39807997/how-to-access-some-widget-attribute-from-another-widget-in-kivy/39808691#39808691) – jligeza Oct 12 '16 at 18:25
  • In the answer you mentioned that binding these two properties is possible in python code. Could you please, tell me how it is done? I would prefer this way over the other (i.e. writing kv code). Thank you for your answer in advance and I really appreciate your help! – Bence Oct 12 '16 at 18:54

1 Answers1

5

As requested in the comments section, I created an example of binding properties between widgets without using the kv lang. Please keep in mind this might not be the best way of accomplishing your goal, as it would be easier to use the kv lang here.

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import mainthread
from kivy.lang import Builder

gui = '''
MyScreenManager

    HomeScreen
        name: 'home'

    LoginScreen
        name: 'login'


<HomeScreen>
    nickname_input: nickname_input
    nickname: nickname_input.text

    BoxLayout:
        TextInput
            id: nickname_input
        Button
            on_press: root.manager.current = 'login'

<LoginScreen>

    BoxLayout:
        Label
            text: root.nickname
        Button
            on_press: root.manager.current = 'home'
'''


class HomeScreen(Screen):
    nickname_input = ObjectProperty()
    nickname = StringProperty()


class LoginScreen(Screen):
    nickname = StringProperty()


class MyScreenManager(ScreenManager):

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

        @mainthread
        def delayed():
            home_screen = self.get_screen('home')
            login_screen = self.get_screen('login')

            home_screen.bind(nickname=login_screen.setter('nickname'))
        delayed()


class Test(App):

    def build(self):
        return Builder.load_string(gui)

Test().run()
jligeza
  • 4,544
  • 6
  • 23
  • 31