Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a kivy StringProperty?

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
like image 769
Bence Avatar asked Oct 12 '16 14:10

Bence


People also ask

What is numeric property in kivy?

Validation for a NumericProperty will check that your value is a numeric type. This prevents many errors early on. Observer Pattern. You can specify what should happen when a property's value changes.

What is object property in kivy?

The Kivy Property is a convenience class similar to Python's own property but that also provides type checking, validation and events. ObjectProperty is a specialised sub-class of the Property class, so it has the same initialisation parameters as it: By default, a Property always takes a default value[.]

How do I install kivy properties?

Open the command terminal with (kvenv), type python -m pip install kivy[full] , and press enter. Note: If you look at Kivy's website, you will see that it says to type “python -m pip install kivy[full] kivy_examples” instead of “python -m pip install kivy[full].” However, both ways will work.


1 Answers

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()
like image 189
jligeza Avatar answered Sep 30 '22 00:09

jligeza