简体   繁体   中英

How to start kivy app with arguments from another kivy app

I want a variable from a textinput to be passed to another class upon start. So I have a class that is a loginscreen (done with python with kivy), when the user enters it's username I want this to be passed along to another class (in a different.py file) so that the newly started class will be able to greet the user with its name.

How do I do this?

login_screen.py:

from kivy.app import App
import user_specific_greeting_screen

class LoginUser():
    def__init__(self):
        pass

def login(self):
    username_to_pass_on = self.user.text
    self.canvas.clear()
    user_specific_greeting_screen.ReturnApp2().run()  # <----  Want to pass var: 
                                                      #         username_to_pass_on
    

class  ReturnApp1(App):
    def build(self):
        return LoginUser()

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

returnapp1.kv:

<LoginUser>:
    user: user_kv
    BoxLayout:
        orientation: 'vertical'
        rows: 2
        BoxLayout:
            TextInput:
                id: user_kv
                hint_text: 'input username'
        BoxLayout:
            Button:
                text: 'Login'
                on_press: root.login()

user_specific_greeting_screen.py

from kivy.app import App

class UserGreet():
    def __init__(self):
        self.label.text = 'Hello ' + username_i_want_passed  # <--- Want username inserted 
                                                             #      here from app_1.py

class ReturnApp2(App):
    def build(self):
        return UserGreet()

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


    

returnapp2.kv:

<UserGreet>:
    label: label_kv
    Label:
        id: label_kv

I hope the code explains what I would like to do. Also, am I thinking about this 'correctly' as a whole? I mean with one.py file for login-screen and another for user-specific-greeting screen etc? Learning both kivy and python and programming at the moment.

Thanks in advance!

Instead of making 2 different python and kivy files, you should use the ScreenManager() in Kivy. This allows you to create multiple Screen() , each with it's own set of widgets in one app.

Python File (main.py)

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


class LoginUserScreen(Screen):

    username_to_pass_on = ""

    def login(self):
        self.username_to_pass_on = self.ids["user_kv"].text
        self.canvas.clear()
        self.manager.current = "UserGreetScreen"


class UserGreetScreen(Screen):
    
    def on_enter(self):
        self.ids["label_kv"].text = 'Hello ' + self.manager.get_screen("LoginUserScreen").username_to_pass_on


class MyApp(App):

    def build(self):
        Builder.load_file("styling.kv")
        sm = ScreenManager()
        sm.add_widget(LoginUserScreen())
        sm.add_widget(UserGreetScreen())
        return sm


MyApp().run()

Kivy File (styling.kv)

#:kivy 1.11.1


<LoginUserScreen>:
    name: "LoginUserScreen"

    BoxLayout:
        orientation: 'vertical'
        rows: 2

        BoxLayout:
            TextInput:
                id: user_kv
                hint_text: 'input username'

        BoxLayout:
            Button:
                text: 'Login'
                on_press: root.login()


<UserGreetScreen>:
    name: "UserGreetScreen"

    Label:
        id: label_kv

Things to note:

  1. When using screens, you can directly access IDs instead of having to create an attribute in the.kv file ( label: label_kv is not needed, you can directly self.ids["<widget id>"] )
  2. You declare Screens in both.py and.kv individually just like the master widget but both in one file
  3. You need to give a name to every screen, this can be different than the class name, but keeping it same helps in consistency
  4. Instead of switching apps, we are now switching screens using self.manager.current = "ScreenName"
  5. I have made the username_to_pass_on , a property of the Screen, allowing easy access from any other screen
  6. We use Builder to load the file and initialize the ScreenManager() in the build, and add the screens one by one and return the manager in the end.
  7. In order to access the other screen's properties and widgets, you use self.manager.get_screen("ScreenName").property from a screen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM